Pointer to a class member
negerns
negerns2000 at gmail.com
Mon Aug 20 22:31:25 PDT 2007
Lutger wrote:
> 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.
Thanks, I tried the writefln(*b).
> 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.
I have a Buffer class which contain the data that i will be manipulating.
class Buffer {
char[] _contents;
uint[] _linelength;
uint[] _linestart;
}
The Buffer class is a member of File class which do file operations.
class FileBuffer {
char[] _filename;
Buffer _buffer;
// File operations
public bool open() { ... }
public bool read() { ... }
public bool save() { ... }
...
}
There's another class that do some analysis and manipulations on the
data that is in the Buffer class. I need to pass the data from the
FileBuffer class without doing a copy operation since the data might be
quite big and I do not want to duplicate the data, do some things and
put it back in. Also, I do not think putting the buffer analysis and
manipulation inside the Buffer class is good since not all Buffer
objects do the analysis and manipulation routines and making that class
a superclass of FileBuffer is, I believe, does not follow good OO design.
class BufferAnalysis() { ... }
What I am thinking right now is something like this:
FileBuffer(Buffer* buffer)
BufferAnalysis(Buffer* buffer)
Let other classes that use the Buffer class have a pointer to the Buffer
to be used and let those classes do what they want with the Buffer data
without making a copy of the data but propagating any changes.
Is this description of what I'm trying to do enough? :P
More information about the Digitalmars-d-learn
mailing list