I
Immortal Nephi
CommandMain derived class is derived from Command1, Command2, and
Command3 derived classes. Command1, Command2, and Command3 derived
classes are derived from robot virtual base class.
I try to assign Robot Pointer to CommandMain. Then Run() function is
always invoked inside CommandMain derived class. How can you use
dynamic_cast? Dynamic_cast should be enabled to invoke Run() inside
either Command1, Command2, or Command3 derived classes.
Please advise…
class Robot
{
public:
Robot() : commandList(0)
{
cout << "Robot()\n";
}
~Robot()
{
cout << "~Robot()\n";
}
virtual void Robot::Run()
{
cout << "Robot::Run()\n";
}
int commandList;
};
class Command1 : virtual public Robot
{
public:
Command1() : Robot()
{
cout << "Command1()\n";
}
~Command1()
{
cout << "~Command1()\n";
}
virtual void Command1::Run()
{
cout << "Command1::Run()\n";
commandList = 1;
}
};
class Command2 : virtual public Robot
{
public:
Command2() : Robot()
{
cout << "Command2()\n";
}
~Command2()
{
cout << "~Command2()\n";
}
virtual void Command2::Run()
{
cout << "Command2::Run()\n";
commandList = 2;
}
};
class Command3 : virtual public Robot
{
public:
Command3() : Robot()
{
cout << "Command3()\n";
}
~Command3()
{
cout << "~Command3()\n";
}
virtual void Command3::Run()
{
cout << "Command3::Run()\n";
commandList = 0;
}
};
class CommandMain : public Command1, public Command2, public Command3
{
public:
CommandMain() : Command1(), Command2(), Command3()
{
cout << "CommandMain()\n";
}
~CommandMain()
{
cout << "~CommandMain()\n";
}
virtual void CommandMain::Run()
{
cout << "CommandMain::Run()\n";
commandList = 0;
}
};
int main()
{
CommandMain list;
Robot* robotPTR = &list;
CommandMain* listPTR = &list;
listPTR->Command1::Run(); // How to invoke Command1::Run() with
robotPTR?
listPTR->Command2::Run(); // How to invoke Command2::Run() with
robotPTR?
listPTR->Command3::Run(); // How to invoke Command3::Run() with
robotPTR?
system("system");
return 0;
}
Command3 derived classes. Command1, Command2, and Command3 derived
classes are derived from robot virtual base class.
I try to assign Robot Pointer to CommandMain. Then Run() function is
always invoked inside CommandMain derived class. How can you use
dynamic_cast? Dynamic_cast should be enabled to invoke Run() inside
either Command1, Command2, or Command3 derived classes.
Please advise…
class Robot
{
public:
Robot() : commandList(0)
{
cout << "Robot()\n";
}
~Robot()
{
cout << "~Robot()\n";
}
virtual void Robot::Run()
{
cout << "Robot::Run()\n";
}
int commandList;
};
class Command1 : virtual public Robot
{
public:
Command1() : Robot()
{
cout << "Command1()\n";
}
~Command1()
{
cout << "~Command1()\n";
}
virtual void Command1::Run()
{
cout << "Command1::Run()\n";
commandList = 1;
}
};
class Command2 : virtual public Robot
{
public:
Command2() : Robot()
{
cout << "Command2()\n";
}
~Command2()
{
cout << "~Command2()\n";
}
virtual void Command2::Run()
{
cout << "Command2::Run()\n";
commandList = 2;
}
};
class Command3 : virtual public Robot
{
public:
Command3() : Robot()
{
cout << "Command3()\n";
}
~Command3()
{
cout << "~Command3()\n";
}
virtual void Command3::Run()
{
cout << "Command3::Run()\n";
commandList = 0;
}
};
class CommandMain : public Command1, public Command2, public Command3
{
public:
CommandMain() : Command1(), Command2(), Command3()
{
cout << "CommandMain()\n";
}
~CommandMain()
{
cout << "~CommandMain()\n";
}
virtual void CommandMain::Run()
{
cout << "CommandMain::Run()\n";
commandList = 0;
}
};
int main()
{
CommandMain list;
Robot* robotPTR = &list;
CommandMain* listPTR = &list;
listPTR->Command1::Run(); // How to invoke Command1::Run() with
robotPTR?
listPTR->Command2::Run(); // How to invoke Command2::Run() with
robotPTR?
listPTR->Command3::Run(); // How to invoke Command3::Run() with
robotPTR?
system("system");
return 0;
}