Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*new is always wrong. ALWAYS

Tags:

c++

So as to explain about pointers and references in this question I wrote this code.

MyClass& MyClass::MyInstance()
{       
    static MyClass & myLocalVariable = * new MyClass(/*parameters*/);
    return myLocalVariable ;
}

one of the comments, by a really impressive high reputation SO user, simply states: *new is always wrong. ALWAYS.

It is the first time I'm told about this: Is it a famous coding standard we all should know about ? What are the reasons behind ?

like image 719
Stephane Rolland Avatar asked Nov 22 '13 13:11

Stephane Rolland


1 Answers

I'm normally pragmatic, however this is too much even for me!

static MyClass & myLocalVariable = * new MyClass(/*parameters*/);

Seriously? Why not simply:

static MyClass myLocalVariable{/*parameters*/};
like image 89
Nim Avatar answered Sep 22 '22 21:09

Nim