Reputation: 17268
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
?
Upvotes: 1
Views: 108
Reputation: 264649
It seems the way to fix it is MyObject* obj = new MyObject(para1);.
Yes. The result of new is always a pointer to an object.
Note in C++ it is very rare to use RAW pointers (you normally put a pointer into a smart pointer so that it will get correctly deleted).
What if I want the object itself rather than the pointer to the object?
MyObject obj(para1) // Creates an normal automatic object
// The destructor for this obect is automatically called
// When the object goes out of scope (even when exceptions are thrown)
I also wonder:
MyObject* obj1 = new MyObject(para1);
If I print out the value of obj1, it would be an address.
If you print this out:
std::cout << obj1; // print the address (normally).
std::cout << (*obj1); // Will print the object/
// Assuming you have defined the output operator.
MyObject obj2(para1);
std::cout << obj2; // Again prints the object
// Assuming you have defined the output operator.
std::cout << &obj2; // prints the address.
Upvotes: 0
Reputation: 103741
Would
MyObject obj(para1);
be the only way that works for me?
Yes, that is the normal way.
What would be the printed-out value for obj2?
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:
std::ostream & operator<<(std::ostream & os, const MyObject & obj);
Upvotes: 4
Reputation: 8729
You've pretty much answered your own question. If you don't want a pointer then don't use new.. If you're new to c++ and memory management, and you really do need to use a pointer, I'd strongly suggest using a smart pointer (e.g. boost::shared_ptr) instead of a raw pointer..
As for the second question, it depends on what you mean by print out. You can't just print out an object, but you can call a method that knows how to print that object out. You'd have to define this method of course. Something like this:
std::ostream& operator<<(std::ostream& os, const MyObject& rhs)
Upvotes: 0
Reputation: 3001
In C++, the new operator returns a pointer to the object. To get the object, you need to dereference the pointer using the * operator (e.g. *obj).
Upvotes: 0
Reputation: 272687
In some cases*, you can also do:
MyObject obj = MyObject(para1);
Unlike in Java, objects don't inherit a toString
method from some base class. So you can't print out the value of obj2
unless you've defined a mechanism to do so. The standard way is to overload operator<<
:
class MyObject
{
public:
int x;
};
std::ostream &operator<<(std::ostream &os, const MyObject &obj)
{
os << obj.x;
return os;
}
...
MyObject obj;
obj.x = 42;
std::cout << obj << "\n";
Upvotes: 3