Continuing our Project
Notes
- Let's start by adding a function to print out the contents of a question.
- We will pass in a QuestionT.
- Notice we can do this
- But notice that the question is wrong
- This is because our question has been "sliced".
- This is sometimes called upcasting as we are casting up the hierarchy
- Ie: the TFQuestionT has been passed in as a QuestionT
- The compiler ignores the additions.
- Passing the two in as a TFQuestion won't work.
- This is downcasting as we are are casting down the hierarchy
- The compiler flags it as a type mismatch.
- But if it did, we would have problems.
- The TFQuestionT would work just fine.
- But the QuestionT would be missing TFQuestionT parts
- If we could force it to occur, we would be calling junk memory.
- Upcasting is sort of ok, downcasting is usually a problem.
- Fortunately we could do something with references or pointers.
- Let's start with passing as a QuestionT reference
- Hopefully it works, but we are back to where we were before.
- This is because by default the compiler will decide function binding at compile time.
- Ie, since it is passed in as a QuestionT, the compiler will call the QuestionT AskQuestion function.
- But we can fix this with
virtual.
- Virtual marks a function as something that can/should be replaced.
- Marking a function as virtual means that in the future, the runtime system will perform the binding, not the compile time system.
- This will fix our problem.
-
virtual void AskQuestion() const;in QuestionT, but only in the .h file
- Good programming practices demand that we mark AskQuestion with
overridein the TFQuestionT class.-
void AskQuestion() const override
-
- A bit of theory (probably discussed before this, but I want it in the notes)
- When a function is marked virtual
- The compiler builds a virtual table or vtable
- Then when a reference or pointer to an object is called the run time system looks up which function to use.
- That way a derived class can be passed in as a base class pointer or reference, and the run time system will call the proper function.
- This costs a few more instructions each time the function is called, but makes polymorphism possible.
- When a function is marked virtual
- By the way, we just saw polymorphism in action.
- Once a function is marked virtual, it remains virtual for all descendants.
- By the way, virtual functions are a way to enforce interface.
- Our QuestionT is somewhat silly.
- What happens if someone builds a QuestionT, not one of the derived classes?
- It really doesn't know what to do.
- There is a solution.
- pure virtual functions.
- In a base class, we can mark a function as pure virtual by setting it equal to 0
-
virtual void AskQuestion() const = 0; - This prohibits us from declaring anything to be a QuestionT, as it is an abstract class.
- Again, pure abstract classes can not be instantiated.
- You can only instantiate instances of a derived class that implements the virtual function.
- But you can have pointers to these.
- You can not declare a virtual constructor.
- If you have a destructor in a class with a virtual function, the destructor should be virtual.
- We don't need this for our project.
- But if you do
- You want to call the RIGHT destructor
- And virtual assures that you do this.
- Example:
- You have a base class that does not have dynamic memory
- You don't provide a destructor but the compiler will.
- You have a derived class with a destructor.
- You pass the derived class into a function as a base class pointer
- The base class destructor will be called, not the derived class destructor
- You have a memory leak!