Replace pointer to member functions?
Both classes of B1 and B2 are derived from class of A. Class of C is
derived from both classes of B1 and B2. I define =93C c;=94. I create
object arrays which is a pointer to class of A. A pointer to class of
A should be able to invoke B1::Run() and B2::Run() instead of class of
C.
How can you do to fix it? You may suggest to remove class C.
Replace from =93virtual public A=94 to =93public A=94. Then declare static=
to
class of A's data members.
My answer is no because I want to use multi-threads. Static to data
members is ideal for single-thread. I want to create two separate
object arrays. Each object array can only have a copy of class A's
data members. Class of B1, B2, and C can share class A's data member
each process.
Thanks=85
#include <stdio.h>
#include <stdlib.h>
class A
{
public:
A();
~A();
void set_a(int x);
int get_a();
virtual void Run();
virtual void Run2();
virtual void Run3();
private:
int m_a;
};
A::A() : m_a(0)
{
printf("A()\n");
}
A::~A()
{
printf("~A()\n");
}
void A::set_a(int x)
{
m_a = x;
}
int A::get_a()
{
return m_a;
}
void A::Run()
{
printf("A::Run()\n");
}
void A::Run2()
{
printf("A::Run2()\n");
}
void A::Run3()
{
printf("A::Run3()\n");
}
class B1 : virtual public A
{
public:
B1();
~B1();
void set_b1(int x);
int get_b1();
virtual void Run();
virtual void Run2();
virtual void Run3();
private:
int m_b1;
};
B1::B1() : m_b1(0)
{
printf("B1()\n");
}
B1::~B1()
{
printf("~B1()\n");
}
void B1::set_b1(int x)
{
m_b1 = x;
}
int B1::get_b1()
{
return m_b1;
}
void B1::Run()
{
printf("B1::Run()\n");
}
void B1::Run2()
{
printf("B1::Run2()\n");
}
void B1::Run3()
{
printf("B1::Run3()\n");
}
class B2 : virtual public A
{
public:
B2();
~B2();
void set_b2(int x);
int get_b2();
virtual void Run();
virtual void Run2();
virtual void Run3();
private:
int m_b2;
};
B2::B2() : m_b2(0)
{
printf("B2()\n");
}
B2::~B2()
{
printf("~B2()\n");
}
void B2::set_b2(int x)
{
m_b2 = x;
}
int B2::get_b2()
{
return m_b2;
}
void B2::Run()
{
printf("B2::Run()\n");
}
void B2::Run2()
{
printf("B2::Run2()\n");
}
void B2::Run3()
{
printf("B2::Run3()\n");
}
class C : public B1, public B2
{
public:
C();
~C();
void set_c(int x);
int get_c();
virtual void Run();
virtual void Run2();
virtual void Run3();
private:
int m_c;
};
C::C() : m_c(0)
{
printf("C()\n");
}
C::~C()
{
printf("~C()\n");
}
void C::set_c(int x)
{
m_c = x;
}
int C::get_c()
{
return m_c;
}
void C::Run()
{
printf("C::Run()\n");
}
void C::Run2()
{
printf("C::Run2()\n");
}
void C::Run3()
{
printf("C::Run3()\n");
}
int main()
{
C c;
A* pa[2][2]; // Define only one pointer to class A
// B1* pb1 = &c; // Not Needed
// B2* pb2 = &c; // Not Needed
// First Process of thread
pa[0][0] = &c; // How to point to B1 instead of C
pa[0][1] = &c; // How to point to B2 instead of C
// Second Process of thread
pa[1][0] = &c; // How to point to B1 instead of C
pa[1][1] = &c; // How to point to B2 instead of C
// First Process of thread
pa[0][0]->Run(); // Invoke B1::Run() instead of C::Run()
pa[0][1]->Run(); // Invoke B2::Run() instead of C::Run()
// Second Process of thread
pa[1][0]->Run(); // Invoke B1::Run() instead of C::Run()
pa[1][1]->Run(); // Invoke B2::Run() instead of C::Run()
system("pause");
return 0;
}