B
Bryan Parkoff
I find an interesting issue that one base class has only one copy for
each derived class. It looks like that one base class will be copied into
three base classes while derived class from base class is executed. It
means that three derived classes are pointed to a separated copy of base
class. I do not allow second and third copy of base class to be created,
but it must remain only first copy of base class.
It allows three derived classes to point and share one base class rather
than three copies of base class. Here is an short example below. After
reading my example code, there is some hints below my example code.
#include <stdio.h>
class Color
{
protected:
Color();
~Color();
unsigned char Get_Red(void);
unsigned char Get_Blue(void);
unsigned char Get_Green(void);
unsigned char m_Red;
unsigned char m_Blue;
unsigned char m_Green;
private:
static int Count;
};
int Color::Count = 0;
inline Color::Color()
{
if (Count == 0)
printf("Created!!\n");
Count++;
printf("Color::Color() %d\n", Count);
m_Red = 1;
m_Blue = 2;
m_Green = 3;
}
inline Color::~Color()
{
printf("Color::~Color() %d\n", Count);
Count--;
if (Count == 0)
printf("Deleted!!\n");
}
inline unsigned char Color::Get_Red(void)
{
return m_Red;
}
inline unsigned char Color::Get_Blue(void)
{
return m_Blue;
}
inline unsigned char Color::Get_Green(void)
{
return m_Green;
}
class A: public Color
{
protected:
A();
~A();
void Run();
};
inline A::A()
{
printf("A::A()\n");
}
inline A::~A()
{
printf("A::~A()\n");
}
inline void A::Run()
{
printf("%d\n",Color::Get_Red());
printf("%d\n",Color::Get_Blue());
printf("%d\n",Color::Get_Green());
}
class B: public Color
{
protected:
B();
~B();
void Run();
};
inline B::B()
{
printf("B::B()\n");
}
inline B::~B()
{
printf("B::~B()\n");
}
inline void B::Run()
{
printf("%d\n",Color::Get_Red());
printf("%d\n",Color::Get_Blue());
printf("%d\n",Color::Get_Green());
}
class C: public Color
{
protected:
C();
~C();
void Run();
};
inline C::C()
{
printf("C::C()\n");
}
inline C::~C()
{
printf("C::~C()\n");
}
inline void C::Run()
{
printf("%d\n",Color::Get_Red());
printf("%d\n",Color::Get_Blue());
printf("%d\n",Color::Get_Green());
}
class Together: public A, public B, public C
{
public:
Together();
~Together();
void Run();
int Run2();
};
inline Together::Together()
{
printf("Together::Together()\n");
}
inline Together::~Together()
{
printf("Together::~Together()\n");
}
inline void Together::Run()
{
A::Run();
B::Run();
// C::Run();
}
inline int Together::Run2()
{
return A::Get_Blue();
}
void main(void)
{
Together together;
together.Run();
return;
}
I am aware that there is some missings inside class such as operator=
and copy constructor functions. I will add them into private class later.
Do you notice that Color class, A class, B class, and C class are all
protected rather than public? Only Together class is allowed to access
Color class, A class, B class, and C class. It is just a OOP writing
practice, but I am unable to find further information from C++ Primer book
how to tell C++ compiler to prevent Color class from being copied.
Please advise.
each derived class. It looks like that one base class will be copied into
three base classes while derived class from base class is executed. It
means that three derived classes are pointed to a separated copy of base
class. I do not allow second and third copy of base class to be created,
but it must remain only first copy of base class.
It allows three derived classes to point and share one base class rather
than three copies of base class. Here is an short example below. After
reading my example code, there is some hints below my example code.
#include <stdio.h>
class Color
{
protected:
Color();
~Color();
unsigned char Get_Red(void);
unsigned char Get_Blue(void);
unsigned char Get_Green(void);
unsigned char m_Red;
unsigned char m_Blue;
unsigned char m_Green;
private:
static int Count;
};
int Color::Count = 0;
inline Color::Color()
{
if (Count == 0)
printf("Created!!\n");
Count++;
printf("Color::Color() %d\n", Count);
m_Red = 1;
m_Blue = 2;
m_Green = 3;
}
inline Color::~Color()
{
printf("Color::~Color() %d\n", Count);
Count--;
if (Count == 0)
printf("Deleted!!\n");
}
inline unsigned char Color::Get_Red(void)
{
return m_Red;
}
inline unsigned char Color::Get_Blue(void)
{
return m_Blue;
}
inline unsigned char Color::Get_Green(void)
{
return m_Green;
}
class A: public Color
{
protected:
A();
~A();
void Run();
};
inline A::A()
{
printf("A::A()\n");
}
inline A::~A()
{
printf("A::~A()\n");
}
inline void A::Run()
{
printf("%d\n",Color::Get_Red());
printf("%d\n",Color::Get_Blue());
printf("%d\n",Color::Get_Green());
}
class B: public Color
{
protected:
B();
~B();
void Run();
};
inline B::B()
{
printf("B::B()\n");
}
inline B::~B()
{
printf("B::~B()\n");
}
inline void B::Run()
{
printf("%d\n",Color::Get_Red());
printf("%d\n",Color::Get_Blue());
printf("%d\n",Color::Get_Green());
}
class C: public Color
{
protected:
C();
~C();
void Run();
};
inline C::C()
{
printf("C::C()\n");
}
inline C::~C()
{
printf("C::~C()\n");
}
inline void C::Run()
{
printf("%d\n",Color::Get_Red());
printf("%d\n",Color::Get_Blue());
printf("%d\n",Color::Get_Green());
}
class Together: public A, public B, public C
{
public:
Together();
~Together();
void Run();
int Run2();
};
inline Together::Together()
{
printf("Together::Together()\n");
}
inline Together::~Together()
{
printf("Together::~Together()\n");
}
inline void Together::Run()
{
A::Run();
B::Run();
// C::Run();
}
inline int Together::Run2()
{
return A::Get_Blue();
}
void main(void)
{
Together together;
together.Run();
return;
}
I am aware that there is some missings inside class such as operator=
and copy constructor functions. I will add them into private class later.
Do you notice that Color class, A class, B class, and C class are all
protected rather than public? Only Together class is allowed to access
Color class, A class, B class, and C class. It is just a OOP writing
practice, but I am unable to find further information from C++ Primer book
how to tell C++ compiler to prevent Color class from being copied.
Please advise.