Solved
MFC CString concatenation failure
Posted on 2008-10-14
If I have a local variable of type CString and then assign to it using the concatenation operator, things work fine:
CString StringFunction1()
{
CString one;
one = "ONE";
return one;
}
CString StringFunction2()
{
CString two;
two = "TWO";
return two;
}
{
CString x;
x = StringFunction1() + StringFunction2();
MessageBox(x);
}
However, if the second function is replaced by a member function of one of my classes,
{
MyClass myobject;
CString x;
x = StringFunction1() + myobject.StringFunction();
MessageBox(x);
}
then no concatenation happens; the messagebox just shows "ONE".
class MyClass
{
public:
MyClass();
CString StringFunction();
};
CString MyClass::StringFunction()
{
return CString("CSF");
}
I've looked on various google pages, and people keep saying you can't do this because you're returning a pointer to a local variable that no longer exists. Why ? Surely "return" has something like operator= semantics, in that it constructs a copy of whatever it is, in the callER's stack space ?