I have an abstract CommandPath class and a series of derived classes, as shown below:
class CommandPath { public: virtual CommandResponse handleCommand(std::string) = 0; virtual CommandResponse execute() = 0; virtual ~CommandPath() {} }; class GetTimeCommandPath : public CommandPath { int stage; public: GetTimeCommandPath() : stage(0) {} CommandResponse handleCommand(std::string); CommandResponse execute(); };
All derived classes have a member variable called 'stage'. I want to create a function in all of them that manipulates the βsceneβ in the same way, so instead of defining it many times, I thought I would create it in the parent class. I moved the βstageβ from the private partitions of all derived classes to the protected CommandPath section and added the function as follows:
class CommandPath { protected: int stage; public: virtual CommandResponse handleCommand(std::string) = 0; virtual CommandResponse execute() = 0; std::string confirmCommand(std::string, int, int, std::string, std::string); virtual ~CommandPath() {} }; class GetTimeCommandPath : public CommandPath { public: GetTimeCommandPath() : stage(0) {} CommandResponse handleCommand(std::string); CommandResponse execute(); };
Now my compiler tells me about the constructor lines that none of the derived classes have a "stage" member. Do I have the impression that protected members are visible to derived classes?
The constructor is the same in all classes, so I can move it to the parent class, but I'm more worried about why the derived classes cannot access the variable.
Also, since I previously used only the parent class for pure virtual functions, I wanted to confirm that this is a way to add a function that will be inherited by all derived classes.
wyatt source share