C++ syntax: A* a; aclass&

I do C and C#. I have a few questions about C++ syntax related to classes.

1
2
    friend class VehState;
    void changeState (VehState* newState); // { mState = newState; } 

Does VehState* mean variable newState is "a pointer to an instance of class VehState"?


void changeState (Vehicle& veh, VehState* newState) { veh.changeState(newState); }

What does the trailing & mean in Vehicle&?


1
2
3
4
5
  class MovingState : public VehState {
  public:
    static MovingState& theMovingState(); // Singleton design pattern
    virtual void disengageGear(Vehicle& veh);
  };

How is this the Singleton design pattern?

Is disengageGear left undefined for class MovingState?


Here's the full sample code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// statevehicle.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

  class Vehicle {
  public:
    Vehicle ();
    // whatever other ctors are needed
    void turnOn(); // { mState->turnOn(*this); }
    void engageGear(); // { mState->engageGear (*this); }
    void disengageGear(); //{ mState->disengageGear (*this); }
    void turnOff(); //{ mState->turnOff (*this); }
    // other operations
  private:
    friend class VehState;
    void changeState (VehState* newState); // { mState = newState; }
  private:
    VehState* mState;
  };

  class VehState {
  public:
    virtual void turnOn(Vehicle&); // allows changing Vehicle object state pointer
    virtual void engageGear(Vehicle&); // same as above

    virtual void disengageGear(Vehicle&);
    virtual void turnOff(Vehicle&);
  protected:
    void changeState (Vehicle& veh, VehState* newState) { veh.changeState(newState); }
  };

  class MovingState : public VehState {
  public:
    static MovingState& theMovingState(); // Singleton design pattern
    virtual void disengageGear(Vehicle& veh);
  };

  class IdleState : public VehState {
  public:
    static IdleState& theIdleState(); // Singleton design pattern
    virtual void engageGear(Vehicle& veh) {changeState(veh, &MovingState::theMovingState()); }
  };

  class OffState : public VehState {
  public:
    static OffState& theOffState(); // Singleton design pattern
    virtual void turnOn(Vehicle& veh) { changeState(veh, &IdleState::theIdleState()); }
  };
  
  // implement default behavior in VehState method implementations
  
  // implementations of Vehicle methods:
  
  Vehicle::Vehicle () :
    mState(&OffState::theOffState()) { }
  void Vehicle::turnOn() {
    mState->turnOn(*this);
  }
  void Vehicle::engageGear() {
    mState->engageGear (*this);
  }
  void Vehicle::disengageGear() {
    mState->disengageGear (*this);
  }
  void Vehicle::turnOff() {
    mState->turnOff (*this);
  }
  void Vehicle::changeState (VehState* newState) {
    mState = newState;
  }

int _tmain(int argc, _TCHAR* argv[])
{
  Vehicle v;
  v.turnOn();

	return 0;
}
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.
Last edited on
Does VehState* mean variable newState is "a pointer to an instance of class VehState"?

Yes

What does the trailing & mean in Vehicle&?

It means veh is a Vehicle passed into the function changeState() by reference. Whenever changeState does something to veh (as in your example, calls a member function), it is operating directly on the caller's instance of Vehicle.

How is this the Singleton design pattern?

The function is static: it can be called regardless of whether any instances of MovingState were created, and it returns an instance of MovingState by reference; presumaly (the source code of that function isn't given) the only instance of MovingState in existence, that is, the singleton instance.

Is disengageGear left undefined for class MovingState?

Yes, the source code posted is missing the definition of that function, among other things.
Topic archived. No new replies allowed.