MyObject obj = new MyObject(para1);
In Java I can always do this while switching to C++ it gives me an error:conversion from ‘MyObject*’ to non-scalar type ‘MyObject’ requested.
It seems the way to fix it is MyObject* obj = new MyObject(para1);.
What if I want the object itself rather than the pointer to the object?
Would MyObject obj(para1);be the only way that works for me?
Since I’m switching from Java to C++, I also wonder:
MyObject* obj1 = new MyObject(para1);
If I print out the value of obj1, it would be an address.
MyObject obj2(para1);
What would be the printed-out value for obj2?
Yes, that is the normal way.
If you haven’t overloaded the correct operator, you would get a compiler error. You define how your object is printed, using a function with the following signature: