Q: How can I make a class act like a value type (such as struct)

Regan Heath regan at netmail.co.nz
Sun May 27 13:50:20 PDT 2007


Myron Alexander Wrote:
> The only way to make the fields private is to have a class so I would 
> like to build my class to work as a value object in the same way that 
> structs do but with information hiding.

What 'value object' behaviour do you want?  As in, how are you using these types?  Example please :)  Something like this...

NamedParameter p("test",5);
NamedParameter q;

q = p;

In the above, is it that you want q to be a copy of p as opposed to both p and q referring to the same object?

If so, D does not allow you to overload assignment and instead you would need to use a copy method or perhaps supply a constructor that copies, eg.

class NamedParameter {
 NamedParameter(NamedParameter rhs) { ..copy rhs to self here.. }
 NamedParameter dup() { ..create new copy of self and return it.. }
}

If you want some other value object behaviour please explain.

Regan Heath


More information about the Digitalmars-d-learn mailing list