what was wrong with struct & class in C++?

Jb jb at nowhere.com
Thu Dec 13 19:49:13 PST 2007


"Walter Bright" <newshound1 at digitalmars.com> wrote in message 
news:fjs7dt$2civ$1 at digitalmars.com...
>>
>> oops, typo, i meant reference counting, obviously.
>> would it be possible to have a way of changing the behaviour of a struct
>> slightly without having to forward all the calls?
>
> The current ideas on doing reference counting in D involve having a struct 
> wrap a class reference. Please note that current C++ reference counting 
> designs do the same thing - C++ offers no efficiency advantage.

Why not have ref counting built in (but optional) for classes, like 'scope' 
is. If the class is defined as such, or the declared as such, then it 
becomes a ref counted object. The ref count could follow the vtable pointer, 
and if it is < 0 then the object is not ref counted, if >= 0 then it is. 
Then any (de)assignments, become a matter of checking the class ref count, 
and a locked inc / dec it if it is a counted class.

Or have ref counted objects as only assignable to ref count referances. Eg..

Foo a;
counted Foo b;
counted Foo c = new Foo();

a = b; // error
b = c; // Ok
c = a; // error

I guess it wouldnt be popular if it added too much overhead to assignments. 
But it can be optimized for the non ref counted situation, the ref counted 
situation would require a lock instruction to be thread safe, and they are 
expensive anyway.

Can the wrapping of a class referance in a struct decrease the ref count on 
scope exit? Eg..

if (somthing)
{
     counted Foo f = this.getFoo();  <- Inc ref count
    // ...
} <-- can the struct wrapping class dec ref count here as f goes out of 
scope?








More information about the Digitalmars-d mailing list