More chapter 10
- Parent constructors and destructors.
- We already have the order of construction:
- Ctor-initializers
- Or member data is constructed in the order it is specified
- The the constructor is called.
- When we throw inheritance on top we get
- Default base class or base class constructor used in ctor-initializer
- This is recursive
- And base class members will be constructed first.
- Then members of the derived class
- Finally the derived class constructor.
- Gregorie points out that you can pass constructor arguments from derived to base, but don't pass member data from derived to base.
- They have not been built yet.
- He also points out that in the constructor, the base class virtual methods will be called.
- Because you are in the base class, so it will look up base class functions.
- The destructors are the opposite
- We know that in regular classes
- Destructor is called.
- Then objects are destroyed in reverse order of creation.
- Adding a parent, the parent destructor is called after this.
- Then the destructor for parent is called.
- He says that base class virtual member functions are called in the base destructor.
- Other items
- When we override a method, we still have access to it.
- Let's take a look at a new person class.
- I have not seen it explicitly declared, but virtual, once attached to a function, is inherited.
- Casting between base and derived classes.
- Upcasting is casting from a derived class to a base class.
- PersonT p = static_cast<PersonT> student;
- There is no "loss" as the derived class contains all of the data of the base class.
- If this is done with references or pointers, slicing is avoided.
- This should be done with a dynamic_cast
- Downcasting is casting from a base class to a derived class.
- Casting from a base class to a derived class might be ok if the object was originally upcasted.
- But it might be highly problematic as well.
- Gregorie warns us that downcasting is a sign of poor design.
- If you have a function that needs the derived class, make it take a reference or pointer to the derived class as a parameter.
- Casts
- This is from starting on page 357
- In c there was a single cast (type) expression
- A little confusing syntax wise.
- You usually ended up (type)(expression)
- On first pass, c++ added type(expression)
- This is find for most simple types.
- But starts to fall apart for unsigned long int ()
- There are four other casts in c++
- const_cast
- static_cast
- reinterpret_cast
- dynamic_cast
- const_cast allows you to change the const-ness of an object.
- This is mostly for using code you don't have control over.
- Only really works with pointers.
- You need to be sure you want to do this.
- It will make it changeable.
- Look at cast1.cpp
- static_cast
- This does the explicit conversions supported by the language.
- It will also use user defined constructors to cast
- This is your workhorse cast.
- You can do an upcast with this.
- But it is not safe.
- Ie it will just do it.
- dynamic_cast
- Uses the vtable
- If the cast makes sense, it will do it.
- If it does not, it will return a null pointer or throw an exception.
- reinterpret_cast
- Pretty much a sledge hammer.
- Turn almost everything into almost anything else.
- Look at cast2.cpp
- Look at cast3.cpp