Copy Constructors, Destructors
- Continuing in chapter 8.
- As we have seen, copy constructors are used to pass by value.
- If you don't want a copy constructor, delete it.
- Reference Variables in general
- Pass by reference, return by reference or otherwise.
- These behave in a way different from other variables.
- The initial assignment is a pointer assignment.
- A pointer to the object is copied to the reference variable.
- After this, all access are a dereference.
- We get the cost of a pointer with the ease of a non-pointer.
- A reference can also be thought of as an alias.
- See simpleRef.cpp
- This is efficient as we have discussed for pass by reference, and pass by constant reference parameters.
- This model has two implications
- We can have member variables that are references.
- But they must be assigned in the ctor-initializer
- Otherwise this is not the first assignment
- This would be useful if every sector needed a pointer back to the map.
- We need to be careful that when we return a reference, we are not returning temporary memory.
- See returnDemo.cpp
- There is more on this in chapter 11.
- Object Destruction
- Objects are destroyed as they are removed from the stack.
- So in reverse order of creation.
- This is true for member objects as well
- The class destructor is called and executed.
- The the destructors for member objects are called
- In reverse order of declaration.
- For my demo I need a static member data
- This is discussed in chapter 9.
- But the idea is just like a state member function
- They exist outside the existence of any particular instance.
- As such, they need to be initialized is a special way
- Either
class::member=value
where you define functions. (Pre c++17)
- Or
static inline type identifer = value
in the declaration.
- See destructor.cpp