This works:
MyObject *o;
o = new MyObject();
And this does not:
MyObject o = new MyObject();
Why?
The keyword new
returns a pointer. It must be assigned to a pointer of an object.
This would also work:
MyObject o = MyObject();
EDIT:
As Seth commented, the above is equivalent to:
MyObject o;
The default constructor (i.e. without parameters) is called if no constructor is given.
Because they're not equivalent. Try:
MyObject* o = new MyObject();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With