Does VehState* mean variable newState is "a pointer to an instance of class VehState"? |
Yes, but in this situation I'd say: "mState is a pointer to an instance of an object who's interface is defined by VehState. Line 56 above is an initializer where mState points to an instance of Offstate. This is legal because Offstate inherits from VehState. mState can only use methods declared by VehState, though the virtual ones may be implemented in Offstate.
What does the trailing & mean in Vehicle&? |
This is a reference. It's somewhat like a pointer, but it's forced to affect only one object (cannot be pointed to anything else) and it does not need to be de-referenced (don't need to use * or -> ). When passed into a function, it's simply passing the original object into a function.
It's a way to do two things:
1) Modify the original object like we would do with a pointer in C,
2) pass a large object into a function without invoking a copy constructor (which can be expensive if you are passing something big).
How is this the Singleton design pattern? |
theMovingState returns a
reference to the original object. If you modify the output of this function, you are modifying the original object and all references to that object.
Is disengageGear left undefined for class MovingState? |
It could be defined in the implementation but doesn't have to be. When it is virtual, it allows a child object to implement this function. If we provide an implementation, that is a default implementation for when a child doesn't implement this. If we don't provide an implementation, then the child who uses this must provide an implementation. If we don't provide an implementation, then the parent class MUST be inherited, it can't be instantiated as it becomes abstract.