lundi 20 juin 2016

Declare and initialize pointer to a class instance in one line


Assuming I have a class MyClass(int i), I know I can do:

int j = 1;
MyClass instance(j);

i.e. declare and initialize instance in one line. Is there a short syntax that allows to do the same for a pointer to MyClass?

MyClass *instance = new MyClass(j);

works, but the syntax has "MyClass" two times, which is a bit redundant.


Aucun commentaire:

Enregistrer un commentaire