Re: How Do I Prevent Copying From Base Class?
From: Anil Mamede (amak_at_mega.ist.utl.pt)
Date: 05/16/04
- Next message: Anil Mamede: "Re: Memory leak"
- Previous message: John Harrison: "Re: strcpy"
- In reply to: Bryan Parkoff: "How Do I Prevent Copying From Base Class?"
- Next in thread: Bryan Parkoff: "Re: How Do I Prevent Copying From Base Class?"
- Reply: Bryan Parkoff: "Re: How Do I Prevent Copying From Base Class?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 16 May 2004 12:34:13 +0000
Bryan Parkoff wrote:
> 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.
>
you could try this:
struct ColorValue {
int green;
int blue;
int red;
};
class Color {
protected:
get_Red() {}
get_Green() {}
get_Blue() {}
// Define the setter. The colorValue will change so if the value is
shared we have to create a new one
set_Red(int red)
{
if(shared) {
// Create a new color value
value = new ColorValue(*value);
shared = false;
}
// Change the color here
value.red = red;
private:
ColorValue* value;
bool shared;
public:
Color()
{
value = ColorValue(green, red, blue); // This will
create a new color value;
shared = false;
}
// Now the important part. The copy constructor
Color(Color& c)
{
m->registerColor(this);
value = c.getColorValue();
shared = true;
}
// Now for the equal
operator=(Color& c)
{
m->registerColor(this);
value = c.getColorValue();
shared = true;
}
- Next message: Anil Mamede: "Re: Memory leak"
- Previous message: John Harrison: "Re: strcpy"
- In reply to: Bryan Parkoff: "How Do I Prevent Copying From Base Class?"
- Next in thread: Bryan Parkoff: "Re: How Do I Prevent Copying From Base Class?"
- Reply: Bryan Parkoff: "Re: How Do I Prevent Copying From Base Class?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|