C++ Pointers and Object Instantiation
问题 This works: MyObject *o; o = new MyObject(); And this does not: MyObject o = new MyObject(); Why? 回答1: 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. 回答2: Because they're not equivalent. Try: MyObject* o = new MyObject(); 回答3: new MyObject() returns a pointer to