Instantiating classes using new [closed]

comming from other programming language why can’t I do this in c++:

Because in C++ to initilize variable types on the left and right side of = must be compatible. In your case:

myClass mc = new myClass();

mc has type myClass and expression on the right has type myClass *. So you either need to change left side to:

myClass *mc = new myClass();

or right side to:

myClass mc = myClass();

to make both sides compatible. What implications it would have on class instance lifetime you should get from a C++ textbook.

Browse More Popular Posts

Leave a Comment