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