struct vs. class containers

bearophile bearophileHUGS at lycos.com
Sun May 23 15:06:32 PDT 2010


Andrei Alexandrescu:

> 1. Using a class makes implementing members easier because there's no 
> need to do work through an additional member. With a struct, you need a 
> pimpl approach. For example:
> 
> struct Array {
>      struct Impl {
>          ...
>      }
>      Impl * impl;
>      ...
>      @property length() const { return impl->length; }
>      ...
> }

I am far from being an expert as you, and while I have read about the PImpl in C++, I don't understand what you are saying here. I am sorry.


> 3. The creation syntaxes are different.

In C# you use 'new' for both structs allocated on the stack and classes allocated on the heap:
http://msdn.microsoft.com/en-us/library/51y09td4%28VS.71%29.aspx


> Let me note that I have reached the conclusion that containers should
> be at best reference types, with a meta-constructor Value!(C) that takes
> a container C and makes it into a value type.

Does Value!() use static introspection and placement new to instantiate the given class on the stack? :-)

Bye and thank you,
bearophile


More information about the Digitalmars-d-announce mailing list