473,396 Members | 1,784 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,396 software developers and data experts.

child class as argument?

Hi,
i have another problem:
i have a container basis class and want to write a virtual "copyFrom()"
function.
Basically the child class should copy from another child class.
************************
class Basis{
public:
virtual void copyfrom(basis & obj)=0;
}
class Child: public Basis{

public:
int x;
void copyfrom(Basis & obj){

//Problem is Basis has no element "x"!!
x=obj.x;
};

}
************************
(i know x should not be public... just to lazy to write getter)
thing is:
how can i write the basis class already so that it'll accept the future
child of it as an argument?
or is it not a problem? can i just do it?
thanks for any answers...

May 15 '06 #1
4 1777
mw****@freenet.de wrote:
Hi,
i have another problem:
i have a container basis class and want to write a virtual "copyFrom()"
function.
Basically the child class should copy from another child class.
************************
class Basis{
public:
virtual void copyfrom(basis & obj)=0;
}
class Child: public Basis{

public:
int x;
void copyfrom(Basis & obj){

//Problem is Basis has no element "x"!!
x=obj.x;
};

}
************************
(i know x should not be public... just to lazy to write getter)
First of all, it's not necessary for a member function, and second it
took just as long to write your disclaimer. ;-)
thing is:
how can i write the basis class already so that it'll accept the future
child of it as an argument?
or is it not a problem? can i just do it?
thanks for any answers...


What you are asking doesn't make a lot of sense to me. Consider this:

class Shape
{
public:
virtual void CopyFrom( const Shape& ) = 0;
};

class Circle : public Shape
{
int radius_;
public:
virtual void CopyFrom( const Shape& );
};

class Polygon : public Shape
{
int sides_, height_;
public:
virtual void CopyFrom( const Shape& );
};

How do you propose to have a circle copy from a polygon when their
implementations are different even in terms of the number and semantic
meaning of their data members? If they were more similar, you could
either move their similarities into the base class or insert an
intermediate base class with their shared properties (which would allow
for other Shapes that don't share in those properties).

You might be seeking the Visitor design pattern.

Cheers! --M

May 15 '06 #2
dj
mlimber wrote:
mw****@freenet.de wrote:
Hi,
i have another problem:
i have a container basis class and want to write a virtual "copyFrom()"
function.
Basically the child class should copy from another child class.
************************
class Basis{
public:
virtual void copyfrom(basis & obj)=0;
}
class Child: public Basis{

public:
int x;
void copyfrom(Basis & obj){

//Problem is Basis has no element "x"!!
x=obj.x;
};

}
************************
(i know x should not be public... just to lazy to write getter)


First of all, it's not necessary for a member function, and second it
took just as long to write your disclaimer. ;-)
thing is:
how can i write the basis class already so that it'll accept the future
child of it as an argument?
or is it not a problem? can i just do it?
thanks for any answers...


What you are asking doesn't make a lot of sense to me. Consider this:

class Shape
{
public:
virtual void CopyFrom( const Shape& ) = 0;
};

class Circle : public Shape
{
int radius_;
public:
virtual void CopyFrom( const Shape& );
};

class Polygon : public Shape
{
int sides_, height_;
public:
virtual void CopyFrom( const Shape& );
};

How do you propose to have a circle copy from a polygon when their
implementations are different even in terms of the number and semantic
meaning of their data members? If they were more similar, you could
either move their similarities into the base class or insert an
intermediate base class with their shared properties (which would allow
for other Shapes that don't share in those properties).

You might be seeking the Visitor design pattern.

Cheers! --M


I suppose the abstract CopyFrom in the base class forces all derived
classes to implement it. How about forward declaring the child class so
you can use it in the declaration of the base class? But this could be
inelegant with many child classes. Perhaps it is better to let the
CopyFrom use the base class argument and use dynamic cast to change it
into any type the derived class can copy from.
May 15 '06 #3
dj schrieb:
I suppose the abstract CopyFrom in the base class forces all derived
classes to implement it.
Yes, thats exactly what it want:
just a unified API for all child classes!
that way i can re-use alot of code afterwards by changing jsut the
child class type.

How about forward declaring the child class so
you can use it in the declaration of the base class? But this could be
inelegant with many child classes.
Yep, there are going to be 5 or 6 different childs. :(
Perhaps it is better to let the
CopyFrom use the base class argument and use dynamic cast to change it
into any type the derived class can copy from.


Thats what i did first... but i am not happy with it since its a public
method. Im "losing face" casting on public level. :D

Now im thinking of just deleting the copy class and put a small
readme.. asking ppl to implement such a class. but thats so dull...

im not sure if the visitor pattern is such good idea.

May 15 '06 #4
mw****@freenet.de wrote:
dj schrieb:
I suppose the abstract CopyFrom in the base class forces all derived
classes to implement it.


Yes, thats exactly what it want:
just a unified API for all child classes!
that way i can re-use alot of code afterwards by changing jsut the
child class type.

How about forward declaring the child class so
you can use it in the declaration of the base class? But this could be
inelegant with many child classes.


Yep, there are going to be 5 or 6 different childs. :(
Perhaps it is better to let the
CopyFrom use the base class argument and use dynamic cast to change it
into any type the derived class can copy from.


Thats what i did first... but i am not happy with it since its a public
method. Im "losing face" casting on public level. :D

Now im thinking of just deleting the copy class and put a small
readme.. asking ppl to implement such a class. but thats so dull...

im not sure if the visitor pattern is such good idea.


Why don't you describe in greater detail what you are trying to do?
(Providing a more substantial code sample could also be helpful.) Then
we might be able to suggest some more options.

Cheers! --M

May 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
2
by: gao_bolin | last post by:
I have the following situation: class A { public: virtual ~A() {} }; template < typename T > class B : public A
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
0
by: dan.jakubiec | last post by:
I'm trying to write a Python app which accepts a socket connection and then spawns another Python process to handle it. I need it to run under both Linux and Windows. I have it working under...
11
by: manstey | last post by:
Hi, I am having trouble designing my classes. I have two classes. The first one wraps around an old-style class called oref Class CacheClass(object): def __init__(self, obj):
2
by: VirGin | last post by:
Hola Folks, I have a class template (or template class, depending on how one was taught). I have a class that is derived from the template class. I have a method in the parent that is overloaded...
3
by: Sirmont | last post by:
Good day all, I am trying to overload (change the argument list) the method of a parent class within the child class. When I do that, the parent class version of the method stops being "declared"....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.