Problems with overriding

T

Thomas Lorenz

Hi

I have a class Person and a class Student which inherits from Person. Each
class should have a method which copies the properties from another Person
or Student respectively. The copy-method of Student should override the one
of Person and call it as well.
It should look like this (which won't compile):

class Person
{
public virtual void CopyFrom(Person source)
{
//Copy properties
}
}

class Student : Person
{
public override void CopyFrom(Student source)
{
//Copy Student properties
}
}

If I don't use the virtual/override modifiers then I have an overload copy
method in Student which i don't want. There should only be one method in
each class with the according parameter.

How would I do that?

Thank you
 
S

Simon Tamman

class Student : Person
{
public override void CopyFrom(Person person)
{
Student student = (Student) person;
// copy student properties
base.CopyFrom(student);
}
}

?
 
N

Nicholas Paldino [.NET/C# MVP]

Thomas,

You can't do it the way you want. I believe this is called covariance.
You have to basically have your CopyFrom method take a Person parameter in
the derived class.
 
E

Eddy Young

Hi

I have a class Person and a class Student which inherits from Person. Each
class should have a method which copies the properties from another Person
or Student respectively. The copy-method of Student should override the one
of Person and call it as well.
It should look like this (which won't compile):

class Person
{
public virtual void CopyFrom(Person source)
{
//Copy properties
}
}

class Student : Person
{
public override void CopyFrom(Student source)
{
//Copy Student properties
}
}

If I don't use the virtual/override modifiers then I have an overload copy
method in Student which i don't want. There should only be one method in
each class with the according parameter.

How would I do that?

Thank you


Why not use a copy constructor?

Eddy
 
S

Simon Tamman

I never recommend a hand-made copy constructor, because as time goes on,
properties change and they're can easily be left out of the copy
constructor.
I'd prefer to provide public access to MemberwiseClone.
 
C

Charles Jenkins

Hi

I have a class Person and a class Student which inherits from Person. Each
class should have a method which copies the properties from another Person
or Student respectively. The copy-method of Student should override the one
of Person and call it as well.


You did not spell out why you want this; if you simply want to clone
Students, like this--

Student doyle = new Student();
// set properties of doyle here, then clone him
Student crabbe = doyle.Clone();
// change some properties of crabbe here

--then IClonaeable is the way to go. OTOH, If you want to clone a
Person into a Student, that's wrong because Person lacks the properties
of Student and thus cannot clone into a fully developed student. You
might try a hierarchy of assignment mehods for that purpose.

Anyway, back to cloning. O'Reilly's book, .NET Gotchas, recommends
something like this:

public class Person :
ICloneable
{
protected Person ( Person other ) {
// Copy Person attributes
}

public virtual object Clone() {
return new Person( this );
}
}


public class Student :
Person
{
protected Student( Student other ) :
base( other ) // <---- This copies Person attributes
{
// Copy Student attributes
}

public override object Clone() {
return new Student( this );
}
}


The key that makes it all work is the PROTECTED copy constructor in each class.
 
E

Eddy Young

Yep. How I'd do it.

You did not spell out why you want this; if you simply want to clone
Students, like this--

Student doyle = new Student();
// set properties of doyle here, then clone him
Student crabbe = doyle.Clone();
// change some properties of crabbe here

--then IClonaeable is the way to go. OTOH, If you want to clone a
Person into a Student, that's wrong because Person lacks the properties
of Student and thus cannot clone into a fully developed student. You
might try a hierarchy of assignment mehods for that purpose.

Anyway, back to cloning. O'Reilly's book, .NET Gotchas, recommends
something like this:

public class Person :
ICloneable
{
protected Person ( Person other ) {
// Copy Person attributes
}

public virtual object Clone() {
return new Person( this );
}
}


public class Student :
Person
{
protected Student( Student other ) :
base( other ) // <---- This copies Person attributes
{
// Copy Student attributes
}

public override object Clone() {
return new Student( this );
}
}


The key that makes it all work is the PROTECTED copy constructor in each class.
 
S

Simon Tamman

But surely you're not recommending?

Person(Person p)
{
this.name = p.Name;
this.title = p.Title;
// etc....
}

I'd go for reflection, or memberwise clone otherwise we're just introducing
a point of maintenance in the application aren't we?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top