Jonathan M Davis jmdavisProg at
Mon Nov 28 02:54:07 PST 2011

On Monday, November 28, 2011 10:28:34 Debdatta wrote:
> Let me get this straight. Instances are shared... and marking a class shared
> marks all its members shared? If what you said were true, it would be
> trivial to instantiate a class as both shared and unshared. Without any
> duplication.
> class A
> {
> ///
> }
> shared A sharedA = new A;
> A unsharedA = new A;
> I don't can you elaborate your last post?

Instantiating it isn't the hard part. You probably need a constructor which is 
shared, but that's not all that big a deal. The problem is that every member 
function that you want to call with a shared instance needs to be marked as 
shared (just like a member function which is called with a const instance must 
be marked as const). And while a const member function may work with both 
mutable and immutable instances, a shared member function will _only_ work 
with shared instances. So, end up having to duplicate any functions that you 
want to be able to call with both shared and unshared instances.

Now, there's a decent chance that the shared versions need to be different 
anyway, because they may need to deal with locking in some manner or the like, 
and the judicious use of template mixins and string mixins can avoid much 
duplication, but you still end up having to duplicate a fair bit, so trying to 
use a class in both a shared and unshared context can get annoying.

- Jonathan M Davis

More information about the Digitalmars-d mailing list