Pointer to a class member

Lutger lutger.blijdestijn at gmail.com
Mon Aug 20 14:03:59 PDT 2007


negerns wrote:
> Please tell me if the following code is doing something illegal or is 
> not a good way to do it.
> 
>   writefln(cast(char[])*b);
> 

This is correct, but not a good way to do it. The type of b is a pointer 
to char[], so writefln(*b) will also work. Casts are there to work 
around the type system but it's better to use them only if they are needed.

But in this situation it may be better to not use pointers at all? I 
don't know how you class design looks like, so ignore this if it isn't 
relevant, but generally a typical getter/setter (a.k.a property) can be 
used like this:

class A {
     this(char[] n) { _name = n; }
     char[] name() { return _name; }
     void name(char[] n) { _name = n; }
     private char[] _name;
}


auto a = new A("digitalmars");

char[] p = a.name;
a.name = p ~ ".com"; // you cannot say a.name~=com, this is a limitation 
of D's properties.

Or if all the data manipulation happens outside the class it may 
indicate that variable should be moved elsewhere.




More information about the Digitalmars-d-learn mailing list