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

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 1791
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**********@discussions.microsoft.com> a écrit dans le
message de news: 37**********************************@microsoft.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(Descendent 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**********@discussions.microsoft.comwrote in message news:43**********************************@microsof t.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
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...
3
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; // ......
3
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...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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,...
0
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...

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.