Initializing With a Base Class Instance

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Jamie said:
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
 
"Jamie Hankins" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

| 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
 
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
 
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
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
 
Back
Top