1)
When I allocate a object of MyClass dynamically, does someObj get allocated on the heap as well?
2)
If I replace the implementation of the constructor to
1 2 3
MyClass::MyClass() {
someObj = SomeClass();
}
does anything change?
3)
If I reasign a pointer-member within a member function of a object to a locally created object, does the pointer get invalid after the function returns? (Statically allocating the object)
4)
If I reasign a (value)-object-member within a memberfunction of a object to a locally created object, does the object get invalid after the function returns?
(Statically allocating the object)
1) Yes. It's contained within the MyClass object, and the entire MyClass object is allocated on the heap.
It would make no sense at all for part of the object to be on the heap, and another part of it not to be.
2) No. You're simply assigning values to someObj which, by this point, has been created on the heap.
To be more thorough: you're creating a temporary, nameless object of SomeClass on the stack, and then assigning someObj to have the same value as the temporary. But someObj is already on the heap, because it's part of the MyClass object.
3) If, by "locally-created", you mean on the stack, then yes. Obviously, when the function returns, the local object is destroyed, so the address stored in the pointer no longer has valid data.
4) Can you explain this a bit better, please? Maybe show some code to illustrate what you're trying to say?
By calling changeObj() I locally create a object and assign it to the member variable. Question is if it gets "delete" when the function returns (see pointers) or stay in memory and everthing is fine.
5)
Is it possible that forward declaring SomeClass in the header doesn't give me a incomplete type error when including the class in the cpp file? Because thats happening to me right now and I thought that using non-pointer/reference types with forward-decl is not permitted?
4) obj is an object and you can't change it to be another object. When you assign another object to obj it copies the object, it doesn't become the other object. The temporary object you get from doing SomeClass() will only stay alive on that line, but that is not a problem because it is only used on that line.
5)
You can have pointers and references to a forward declared class but as soon as you are accessing any of the members (functions or variables) you need the full class definition. Often a forward declaration is enough in the header but in the source file, where you actually use the objects, an include is needed.
What happens is:
1. A temporary object is constructed and initialised by passing 2 to the SomeClass::SomeClass(int) constructor (or similar).
2. The copy assignment operator SomeClass& SomeClass::operator=(const SomeClass&) is called on obj passing the temporary object as argument.
3. The temporary object is destructed and the destructor SomeClass::~SomeClass() is called.
Exactly what these functions do depends on the class SomeClass and how they are defined.
4) By calling changeObj() I locally create a object and assign it to the member variable. Question is if it gets "delete" when the function returns (see pointers) or stay in memory and everthing is fine.
You're not thinking about it precisely. The member variable is created as part of the MyClass object, which means it's on the stack.
Then, during your changeObj method, you are doing the following:
- Creating a temporary, unnamed object of type SomeClass on the stack, in exactly the same way as you did in 2)
- Calling the assignment operator on your obj member, which should set the state of the obj object (which is already created on the stack) based on the state of the temporary object - in exactly the same way as it did in 2)
The line obj = SomeClass(2); simply changes the state of obj. It doesn't somehow destroy obj and replace it with a new object.
So after the call obj holds a copy of the temporary constructed object. The temporary constructed object get's destructed though?
Correct - assuming that the assignment operator for SomeClass simply copies the values of all the data members. And if you haven't created an assignment operator for it, the compiler will create a default one that does exactly that.
Well I come from Java and reasigning does change the object meaning it unasigns the old object from the reference-variable and asigns a new object to it.
I'm still not entierly sure if I understand this correctly.
Well I come from Java and reasigning does change the object meaning it unasigns the old object from the reference-variable and asigns a new object to it.
I wouldn't know.
I'm still not entierly sure if I understand this correctly.