kindly, some questions to answer and statements to confirm or to amend in case. I want to make sure I got it right and I am thankful for any hint. Further I could imagine that those examples as a whole have some value for C++ beginners.
- Stack initiated object
MyClass c(10);
MyClass c = MyClass(10);
To my understanding these two object initializations can be used interchangeably, correct ? Further they are automatically cleaned up when they go out of scope, for example returning form a function.
- Heap initiated object
MyClass* c = new MyClass(10);
This object needs to be cleaned up manually, like "delete c".
- The object bug function
MyClass* getObj() {
MyClass c(10); // stack initiated object
return &c;
}
This will return a pointer to my stack scoped MyClass object (as I am not using the new keyword). And as a matter of fact I should not be able to use the pointer returned to it as at the time of usage the object might be cleaned up already or fail later. Correct ?
- The valid object copy function
MyClass getObj() {
MyClass c(10); // stack initiated object
return c;
}
In this case a copy of the stack initiated object will be returned valid for the scope of the caller, correct ?
Thank you in advance.
Kind regards, Hermann