473,785 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing With a Base Class Instance

I'm probably being dense here. In the following situation:

class Base {
int x;
int y;
}

class Decendant : Base {
int z;
}

I need a function (constructor or whatever) in "Decendant" that will take a
"Base" and do a member-wise copy of "Base"'s members to itself. I know I
could just copy each base member, but I know there's got to be a better way.

I tried the following constructor in Decendant:

public Decendant(Base _base) : base(_base) {
z = 0;
}

The problem is that "Base" doesn't have a constructor that takes an instance
of itself. I'd prefer not to modify "Base", if possible.

I looked at MemberwiseClone , but that doesn't look like what I need.

Thanks in Advance!

Jamie
Jun 28 '06 #1
4 1806
Jamie Hankins wrote:
I'm probably being dense here. In the following situation:

class Base {
int x;
int y;
}

class Decendant : Base {
int z;
}

I need a function (constructor or whatever) in "Decendant" that will take a
"Base" and do a member-wise copy of "Base"'s members to itself. I know I
could just copy each base member, but I know there's got to be a better way.

I tried the following constructor in Decendant:

public Decendant(Base _base) : base(_base) {
z = 0;
}

The problem is that "Base" doesn't have a constructor that takes an instance
of itself. I'd prefer not to modify "Base", if possible.

I looked at MemberwiseClone , but that doesn't look like what I need.

Thanks in Advance!

Jamie

I don't think there is an easy way to do this.
You could use reflection if you wanted to.
You might consider having a constructor (public or protected) on base
that takes an instance of base and initializes itself off the instance
just to keep things in their "proper" place :)

Cheers
JB
Jun 28 '06 #2
"Jamie Hankins" <Ja**********@d iscussions.micr osoft.com> a écrit dans le
message de news: 37************* *************** **...icrosof t.com...

| I'm probably being dense here. In the following situation:
|
| class Base {
| int x;
| int y;
| }
|
| class Decendant : Base {
| int z;
| }
|
| I need a function (constructor or whatever) in "Decendant" that will take
a
| "Base" and do a member-wise copy of "Base"'s members to itself. I know I
| could just copy each base member, but I know there's got to be a better
way.
|
| I tried the following constructor in Decendant:
|
| public Decendant(Base _base) : base(_base) {
| z = 0;
| }
|
| The problem is that "Base" doesn't have a constructor that takes an
instance
| of itself. I'd prefer not to modify "Base", if possible.

The best way of doing this is to add a copy constructor to the base class :

public class Base
{
private int x = 0;
private int y = 0;

... // public properties

public Base() { }

public Base(Base other)
{
x = other.x;
y = other.y;
}
}

public class Descendent : Base
{
private int z = 0;

... // public properties

public Descendent() : base() { }

public Descendent(Desc endent other) : base(other)
{
z = other.z;
}
}

Why would you try and avoid modifying the base class ? Copy constructors are
a common feature in any hierarchy that implements IClonable; in fact, I
believe C++ creates a default copy constructor anyway.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 28 '06 #3
In my real-world problem, "Base" is a class with tons of members, and I don't
want to introduce a new maintenance issue of keeping the copy constructor in
sync with new members. Also, "Base" is in code that another developer owns.

I think you're ultimately right, though. I'm going to e-mail the developer
that owns "Base" (obviously not its real name) and ask about adding a copy
constructor. If not, then I'll just rework "Decendant" to contain a "Base"
as a member instead of inheriting from it.

Thanks for the suggestions.

Jamie

Why would you try and avoid modifying the base class ? Copy constructors are
a common feature in any hierarchy that implements IClonable; in fact, I
believe C++ creates a default copy constructor anyway.

Joanna


Jun 28 '06 #4
JB suggested that you could use reflection. Since Base has a bunch of member variables - the copy constructor could use reflection to get the set of member variables - and then iterate through them all. That way, the copy constructor will be compact, will not have to know explicitly about any member variable and, perhaps most valuably, will dynamically handle new member variables as they are added.

Chris TR
"Jamie Hankins" <Ja**********@d iscussions.micr osoft.comwrote in message news:43******** *************** ***********@mic rosoft.com...
In my real-world problem, "Base" is a class with tons of members, and I don't
want to introduce a new maintenance issue of keeping the copy constructor in
sync with new members. Also, "Base" is in code that another developer owns.

I think you're ultimately right, though. I'm going to e-mail the developer
that owns "Base" (obviously not its real name) and ask about adding a copy
constructor. If not, then I'll just rework "Decendant" to contain a "Base"
as a member instead of inheriting from it.

Thanks for the suggestions.

Jamie

Why would you try and avoid modifying the base class ? Copy constructors are
a common feature in any hierarchy that implements IClonable; in fact, I
believe C++ creates a default copy constructor anyway.

Joanna
Jul 2 '06 #5

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

Similar topics

50
6382
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
3
3495
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ... more protected fields }
3
5711
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I have a base class with several sub classes. In runtime I want to create a instance of a sub class and assign it to a base class pointer. Nothing fancy about that. I also want to be able in runtime to decide witch type of sub class that is to be...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.