By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

How Do I Prevent Copying From Base Class?

P: n/a
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.

--
Bryan Parkoff
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ian
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.

It sound like you are tying to do something that can't be done with
inheritance.

The base class becomes part of and derived class, so a single instance
makes no sense.

If you want there to be a single instance of some class data, use a
static class or struct in the base to contain it.

Ian

<code snipped>
Jul 22 '05 #2

P: n/a
Jim,

If static class is only an option, please give me an example.

--
Bryan Parkoff
"Ian" <no***@nowhere.com> wrote in message
news:10***************@drone5.qsi.net.nz...
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.

It sound like you are tying to do something that can't be done with
inheritance.

The base class becomes part of and derived class, so a single instance
makes no sense.

If you want there to be a single instance of some class data, use a
static class or struct in the base to contain it.

Ian

<code snipped>

Jul 22 '05 #3

P: n/a
Ian
Bryan Parkoff wrote:
Jim,

If static class is only an option, please give me an example.

class Base
{
struct StuffYouOnlyWantOneCopyOf
{
};

static StuffYouOnlyWantOneCopyOff stuffYouOnlyWantOneCopyOf;
};
Jul 22 '05 #4

P: n/a
"Bryan Parkoff" <br******************@nospam.com> 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.


First, let's talk about writing proper tests. I cut your code from here
and paste it into my compiler, run it and get a bunch of stuff as
output. There is no way for me to see what it is that you are trying to
do from this output, I can only see what is happening. Better would be
to put in some asserts and such.

Second, when writing real code, you should obviously put header stuff in
the header file, and source stuff in the source file; but when putting
code on a newsgroup, I, at least, find it easer if the functions are
embedded in the class.

OK, now to the actual question you are asking... It sounds as though you
only want Together to have a single Color base object rather than one
for each itermediate type. Is that correct? I'm going to assume yes, if
I'm wrong then stop reading now, but maybe this will help someone who
does want this...

Let's start with a test that shows what we want.

int main() {
A a[100];
for (int x = 0; x < 100; ++x)
for (int y = x+1; y < 100; ++y)
if ( a[x].getID() == a[y].getID() ) {
cout << "a[" << x << "] has same id as b[" <<
y << "]" << endl;
exit( -1 );
}
// test that objects copied from others have a unique id
A b(a[0]), c;
assert( b.getID() != a[0].getID() );
c = b;
assert( b.getID() != c.getID() );

D d;
assert( d.getBID() == d.getCID() );

cout << "OK";
}

If the above main prints "OK" to the output, then we know we have what
we want.

I ended up with something like this:

#include <iostream>
using namespace std;

class A {
public:
int getID() { return reinterpret_cast<int>(this); }
};

class B: virtual public A { };
class C: virtual public A { };

class D: public B, public C {
public:
int getBID() { return B::getID(); }
int getCID() { return C::getID(); }
};

int main() {
A a[100];
for (int x = 0; x < 100; ++x)
for (int y = x+1; y < 100; ++y)
if ( a[x].getID() == a[y].getID() ) {
cout << "a[" << x << "] has same id as b[" <<
y << "]" << endl;
exit( -1 );
}
// test that objects copied from others have a unique id
A b(a[0]), c;
assert( b.getID() != a[0].getID() );
c = b;
assert( b.getID() != c.getID() );
cout << "OK";

D d;
assert( d.getBID() == d.getCID() );
}
I hope this sheds some light on your question...
Jul 22 '05 #5

P: n/a
Daniel,

I want to say thank you very much for the answer, but it does answer my
question correctly. Why do I need "int getID() { return
reinterpret_cast<int>(this); }"? I am aware that it will give an address of
"this" pointer to each class. I used debug feature so I can see an
hexidecimal address in the variable output. Do you think that "return this
pointer" would help to debug rather than using debug window? Why do I need
assert function? Please explain.
Class A, Class B, and Class C are derived from Color Class that they can
cause Color Class to be copied into three separated Color Class that is
called multiple inheritance. It can waste space if there are more than a
copy of Color Class. You did not explain the word "virtual", but you did
put "virtual" in your example code. The virtual keyword forces Class A,
Class B, and Class C to use only one or single inheritance rather than
multiple inheritance. It allows three derived classes to "point indirectly"
to Color Class.

Let me give you the "virtual keyword" definition.

Virtual base classes offer a way to save space and avoid ambiguities in
class hierarchies that use multiple inheritance.

Each nonvirtual object contains a copy of the data members defined in
the base class. This duplication wastes space and requires you to specify
which copy of the base class members you want whenever you access them.

When a base class is specified as a virtual base, it can act as an
indirect base more than once without duplication of its data members. A
single copy of its data members is shared by all the base classes that use
it as a virtual base.

The virtual keyword appears in the base lists of the derived classes,
not the base class.
It makes sense to me. Thank you again for the answer.

--
Bryan Parkoff
"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news1.west.e arthlink.net...
"Bryan Parkoff" <br******************@nospam.com> 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 intothree 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 ratherthan three copies of base class. Here is an short example below. After
reading my example code, there is some hints below my example code.


First, let's talk about writing proper tests. I cut your code from here
and paste it into my compiler, run it and get a bunch of stuff as
output. There is no way for me to see what it is that you are trying to
do from this output, I can only see what is happening. Better would be
to put in some asserts and such.

Second, when writing real code, you should obviously put header stuff in
the header file, and source stuff in the source file; but when putting
code on a newsgroup, I, at least, find it easer if the functions are
embedded in the class.

OK, now to the actual question you are asking... It sounds as though you
only want Together to have a single Color base object rather than one
for each itermediate type. Is that correct? I'm going to assume yes, if
I'm wrong then stop reading now, but maybe this will help someone who
does want this...

Let's start with a test that shows what we want.

int main() {
A a[100];
for (int x = 0; x < 100; ++x)
for (int y = x+1; y < 100; ++y)
if ( a[x].getID() == a[y].getID() ) {
cout << "a[" << x << "] has same id as b[" <<
y << "]" << endl;
exit( -1 );
}
// test that objects copied from others have a unique id
A b(a[0]), c;
assert( b.getID() != a[0].getID() );
c = b;
assert( b.getID() != c.getID() );

D d;
assert( d.getBID() == d.getCID() );

cout << "OK";
}

If the above main prints "OK" to the output, then we know we have what
we want.

I ended up with something like this:

#include <iostream>
using namespace std;

class A {
public:
int getID() { return reinterpret_cast<int>(this); }
};

class B: virtual public A { };
class C: virtual public A { };

class D: public B, public C {
public:
int getBID() { return B::getID(); }
int getCID() { return C::getID(); }
};

int main() {
A a[100];
for (int x = 0; x < 100; ++x)
for (int y = x+1; y < 100; ++y)
if ( a[x].getID() == a[y].getID() ) {
cout << "a[" << x << "] has same id as b[" <<
y << "]" << endl;
exit( -1 );
}
// test that objects copied from others have a unique id
A b(a[0]), c;
assert( b.getID() != a[0].getID() );
c = b;
assert( b.getID() != c.getID() );
cout << "OK";

D d;
assert( d.getBID() == d.getCID() );
}
I hope this sheds some light on your question...

Jul 22 '05 #6

P: n/a
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;
}
Jul 22 '05 #7

P: n/a
Anil,

No, it is not an answer solution. I have already posted the answer that
Daniel gave an example. Virtual Inheritance is only a solution rather than
multiple Inheritance. Your example code is not a good idea. Please go and
read my another post that it already has an answer.
Think about Color Class that it may have more than 16 color variables
while RGB() is built in inside Color Class. It makes sense if you want to
adjust 16 colors.

--
Bryan Parkoff
"Anil Mamede" <am**@mega.ist.utl.pt> wrote in message
news:40***********************@news.telepac.pt...
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;
}

Jul 22 '05 #8

P: n/a
"Bryan Parkoff" <br******************@nospam.com> wrote:
I want to say thank you very much for the answer, but it does answer my
question correctly. Why do I need "int getID() { return
reinterpret_cast<int>(this); }"?
So you can write code that can check to insure that the D class only
contains one A sub-object.

I am aware that it will give an address of
"this" pointer to each class. I used debug feature so I can see an
hexidecimal address in the variable output. Do you think that "return this
pointer" would help to debug rather than using debug window? Why do I need
assert function? Please explain.


Yes, making it so the code can check iteself is better. By returning the
this pointer and using asserts, I made it perfectly clear what I
expected from the code. Spitting output to a debug window doesn't make
that clear. Output to the debug window requires a human to examine the
output, know what he is looking for, and make sure that it is correct.
By having the code make the check, I was able to examine 100 different A
objects to assure that all of them had unique IDs. Imagine doing that by
eye?
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.