473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How Do I Prevent Copying From Base Class?

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
8 2830
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
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
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
"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
3
by: Tony Maresca | last post by:
Hi. I have a class derived from a UserControl, that I want to allow others to derive controls from. I don't want them to design the base class (which is derived from a UserControl). I know that...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
5
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member...
2
by: Frederick Gotham | last post by:
Is there any way to prevent hiding of a base class function? I want the derived class to add an overload of the function, rather than hide the base class one. The following snippet demonstrates...
3
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.