Hiding class pointers -- operator new()

C. Dunn cdunn2001 at gmail.com
Thu Aug 16 12:00:49 PDT 2007


Walter Bright Wrote:

> James Dennett wrote:
> > Walter Bright wrote:
> >> Value types are fundamentally different from reference types. D gives
> >> you the choice.
> > 
> > C++ gives you *more* choice by allowing easier migration between
> > the two without imposing performance or syntactic differences.
> 
> That isn't my experience. In my work on DMDscript in D, I changed some 
> types between struct and class to try out some variations, and found it 
> to be a lot easier than in C++. For one thing, I didn't have to find and 
> edit all the -> and .'s.


But Walter, how did you handle operator new()?  It needs to be agnostic about class/struct.

class C{int x;};
stuct S{int x;};
typedef C MyType;
//typedef S* MyType;
MyType inst = new MyType;

If you use the second typedef, this will not compile!  That is the problem.

Why not provide an operator New() which expects a pointer type when used for structs?  Just have the compiler translate it.  I cannot see how to write such a generic operator New() myself.

This one small change would be a great boon to C/C++ programmer thinking of adopting D.



More information about the Digitalmars-d mailing list