Original Post
I know this is a newbie-ish question, but I just want to make sure I have this right to avoid any hard-to-track bugs. Lets say one has the following situation:
class MyClass {
public:
int data;
MyClass () : data (5) {}
~MyClass () {}
};
MyClass getClass (){
MyClass temp;
temp.data = 123;
return temp;
}
The focus of this example is the function getClass(). Is that ok? It seems to me I heard somewhere that temp''s destructor would be called and the class would go out of scope after the function ends. That is obvious. But the function returns a copy of temp, so the caller would have a viable copy of the class, correct?
// in some function somewhere MyClass c = getClass (); // use ''c'' somewhereThe above should be ok. Is this the same with structs as well as classes? I don''t see why there would be much of a difference. I know that returning the entire class is slow because all of it''s members have to be copied over to the returning variable, but if the class is simplistic, it shouldn''t be much of an issue. Also, if the class has any special members (like pointers to other classes) that are deleted during the class'' destructor, that would cause a problem, too, right? Thanks for the clarification.