C++ reference type

David Medlock noone at nowhere.com
Sat Jun 24 08:21:39 PDT 2006


Tom S wrote:
> David Medlock wrote:
> 
>> The attribute functions in classes serve this purpose do they not?
>>
>> C++:
>> int& my_attr();
>>
>> D:
>> void my_attr( int );
>>
>> use, in both cases:
>> foo.my_attr = 100;
> 
> 
> Unless you want to be able to say 'foo.my_attr++;'
> or 'foo.someStruct.bar = 1;'
> 
> The latter can be accomplished by making the function return a pointer 
> to the struct, but then you cant say 'Foo x = my_attr();'
> 
> 
Ok, but what exactly does that buy you?

If you want reference semantics use a class.
If you want opPostInc use a struct.

Even without you could just as easily say:

int bar(int r) { return this.someStruct.bar = r; }

if you have several 'bars' perhaps its time to refactor?

Its like saying : I cant ride my car on bike trails, even though I can 
ride my bike on roads.  Just use the bike.

-DavidM



More information about the Digitalmars-d mailing list