wrote:
> I have a class CString. I'm wondering if it's possible to make a global
> function mystr_cat that does this:
>
> CString s1 = "hello";
> s1 = mystr_cat("another", "string", "here");
>
> Thus mystr_cat needs to access the "this" part of s1. Or maybe = can be
> overloaded? Or is this type of thing not allowed?
That may be possible by overloading operator=, but it is very unintuitive. A
user of your class would normally expect operator= to completely replace
the contents of your string. Why not simply:
s1.cat("another", "string", "here");
? Then you can simply make it a member function. Or alternatively, to elide
the need for lots of overloads for different argument numbers or variable
argument lists, you could do something like:
s1.cat("another").cat("string").cat("here");
by making a function like:
CString& CString::cat(const char* arg)
{
//...append arg to your string
return *this;
}