Reassign two objects to one pointer?

I

Immortal Nephi

I am still working on diamond shape inheritance. I am trying to
figure out how to change one pointer between two objects. I always
tell C++ Compiler to select Center1:: or Center2::. I do not want to
declare one pointer with either derived classes. For example, I want
to stick
B_PTR->Run() instead of B_PTR->Center1::Run(). I help to keep
unmodified code in the function body and I simply need to reassign
pointer. Also, I will need to invoke pointer to member function as
well. Look at main() for example.

class Top
{
public:
Top();
~Top();

void Run();

int m_a;
};

class Center1 : virtual public Top
{
public:
Center1();
~Center1();

void Run();
void Center1_Run_1();
void Center1_Run_2();
void Center1_Run_3();
void Center1_Run_4();

void (Center1::*Run_PTR)();
};

class Center2 : virtual public Top
{
public:
Center2();
~Center2();

void Run();
void Center2_Run_1();
void Center2_Run_2();
void Center2_Run_3();
void Center2_Run_4();

void (Center2::*Run_PTR)();
};

class Bottom : public Center1, public Center2
{
public:
Bottom();
~Bottom();

void Run();
};

Top::Top()
{
cout << "Top()\n";
}

Top::~Top()
{
cout << "~Top()\n";
}

void Top::Run()
{
cout << "Top::Run()\n";
}

Center1::Center1()
{
cout << "Center1()\n";
Run_PTR = &Center1::Center1_Run_1;
}

Center1::~Center1()
{
cout << "~Center1()\n";
}

void Center1::Run()
{
cout << "Center1::Run()\n";
}

void Center1::Center1_Run_1()
{
cout << "Center1_Run_1()\n";
Run_PTR = &Center1::Center1_Run_2;
}

void Center1::Center1_Run_2()
{
cout << "Center1_Run_2()\n";
Run_PTR = &Center1::Center1_Run_3;
}

void Center1::Center1_Run_3()
{
cout << "Center1_Run_3()\n";
Run_PTR = &Center1::Center1_Run_4;
}

void Center1::Center1_Run_4()
{
cout << "Center1_Run_4()\n";
Run_PTR = &Center1::Center1_Run_1;
}

Center2::Center2()
{
cout << "Center2()\n";
Run_PTR = &Center2::Center2_Run_1;
}

Center2::~Center2()
{
cout << "~Center2()\n";
}

void Center2::Run()
{
cout << "Center2::Run()\n";
}

void Center2::Center2_Run_1()
{
cout << "Center2_Run_1()\n";
Run_PTR = &Center2::Center2_Run_2;
}

void Center2::Center2_Run_2()
{
cout << "Center2_Run_2()\n";
Run_PTR = &Center2::Center2_Run_3;
}

void Center2::Center2_Run_3()
{
cout << "Center2_Run_3()\n";
Run_PTR = &Center2::Center2_Run_4;
}

void Center2::Center2_Run_4()
{
cout << "Center2_Run_4()\n";
Run_PTR = &Center2::Center2_Run_1;
}


Bottom::Bottom()
{
cout << "Bottom()\n";
}

Bottom::~Bottom()
{
cout << "~Bottom()\n";
}

void Bottom::Run()
{
cout << "Bottom::Run()\n";
}


int main()
{
Bottom b;
Center1* C1_PTR = &b;
Center2* C2_PTR = &b;
Bottom* B_PTR = 0;

B_PTR = static_cast< Bottom* >( C1_PTR );

( B_PTR->*( B_PTR->Run_PTR ) )();
B_PTR->Run(); // Invoke Center1::Run()
// B_PTR->Center1::Run(); // I do not want it, but stick B_PTR->Run()

// Change pointer between derived classes


B_PTR = static_cast< Bottom* >( C2_PTR );

( B_PTR->*( B_PTR->Run_PTR ) )();
B_PTR->Run(); // Invoke Center2::Run()
// B_PTR->Center2::Run(); // I do not want it, but stick B_PTR->Run()

return 0;
}
 
B

Bart van Ingen Schenau

Immortal said:
I am still working on diamond shape inheritance. I am trying to
figure out how to change one pointer between two objects. I always
tell C++ Compiler to select Center1:: or Center2::. I do not want to
declare one pointer with either derived classes. For example, I want
to stick
B_PTR->Run() instead of B_PTR->Center1::Run(). I help to keep
unmodified code in the function body and I simply need to reassign
pointer. Also, I will need to invoke pointer to member function as
well. Look at main() for example.
You are well advised to seriously rethink your design. What you want is
simply not possible in C++.

int main()
{
Bottom b;
Center1* C1_PTR = &b;
Center2* C2_PTR = &b;
Bottom* B_PTR = 0;

B_PTR = static_cast< Bottom* >( C1_PTR );

( B_PTR->*( B_PTR->Run_PTR ) )();

This line is ambiguous, because B_PTR has two accessible members called
Run_PTR. You must explicitly specify if you want the version from
Center1 or from Center2.
B_PTR->Run(); // Invoke Center1::Run()

No. That invokes Bottom::Run.
// B_PTR->Center1::Run(); // I do not want it, but stick B_PTR->Run()

Too bad you don't want it, because it is the easiest way to invoke
Center1::Run when you have a Bottom*.
The only other way is to cast the pointer first to a Center1*:
static_cast said:
// Change pointer between derived classes


B_PTR = static_cast< Bottom* >( C2_PTR );

If you print the value of B_PTR before and after this assignment, you
will notice that both values are identical.
( B_PTR->*( B_PTR->Run_PTR ) )();
B_PTR->Run(); // Invoke Center2::Run()
// B_PTR->Center2::Run(); // I do not want it, but stick B_PTR->Run()

return 0;
}

Bart v Ingen Schenau
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top