Fix Virtual Base Class Pointer

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;
}
 
N

Neelesh

        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?


dynamic cast lets you "cast down" a polymorphic inheritance hierarchy.
In the current case, you can say:

dynamic_cast<CommandMain*>(robotPTR)->Command1::Run();

For readability, you can define another pointer robotPTR2 and use it:

CommandMain* robotPTR2 = dynamic_cast<CommandMain*>(robotPTR);
robotPTR2->Command1::Run();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top