Pointer to a class member

Mike Parker aldacron71 at yahoo.com
Tue Aug 21 09:09:41 PDT 2007


negerns wrote:
> 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
> 

You don't need pointers for this. Unlike in C++, classes in D are 
automatically references. Thus this should do what you want:

FileBuffer(Buffer buffer);
BufferAnalysis(Buffer buffer);

Structs are different and are passed by value, meaning they are copied. 
So when using structs to achieve the same result, you should use the ref 
keyword:

struct MyStruct
{
     ubyte[] contents;
}

SomeFunc(ref MyStruct ms);

But actually, arrays in D are just an object with two fields -- a size 
and a pointer. So even when passing MyStruct by value, you aren't 
copying the data that contents points do, just the size and pointer (8 
bytes on a 32-bit system). Also, you might consider using byte or ubyte 
for the type of your contents array, rather than char.


More information about the Digitalmars-d-learn mailing list