Newbie: copy, assignment of class instances

Larry Luther larry.luther at dolby.com
Thu May 27 12:40:02 PDT 2010


"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:ht4g3r$vue$1 at digitalmars.com...
| Larry Luther:
|
| > I did not get an error when building and running with DMD 2.042:
|
| I am using dmd v2.046, and I have taken the good habit of compiling 
with -w (warnings on).
| It seems this error I see is a blocking warning. It's a warning badly 
written, but do you agree it is saying something important? I presume your 
code can lead to bugs. I don't know why this is a warning instead of a true 
error...
|
| On your code it also complains for a missed "override" that is a good 
thing that eventually will become obligatory even without -w.
|
| When you post here I suggest you to avoid using HTML and use pure text.
|
| Bye,
| bearophile

Ok, I've added -w to compilation commands and I've switched back to pure 
text.

Given:

class A {
  int x, y;

  void copy (const A a) {
    x = a.x;
    y = a.y;
  }
}

class B : A {
  int z;

  void copy (const B b) {
    super.copy( b);
    z = b.z;
  }
}

A alpha = new A;
A bravo = new A;

B charlie = new B;
B delta = new B;

-------------------------------

I don't see the problem with A.copy vs B.copy.
  alpha.copy( bravo)    -- should execute A.copy
  alpha.copy( charlie)  -- should execute A.copy
  charlie.copy( delta)  -- should execute B.copy
  charlie.copy( alpha)  -- should execute A.copy

What am I missing?

Larry




More information about the Digitalmars-d-learn mailing list