C ++指针和对象实例化 [英] C++ Pointers and Object Instantiation
本文介绍了C ++指针和对象实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有效:
MyObject *o;
o = new MyObject();
这不是:
MyObject o = new MyObject();
为什么?
推荐答案
The keyword new
returns a pointer. It must be assigned to a pointer of an object.
这也可以:
MyObject o = MyObject();
如塞思所言,以上等同于:
As Seth commented, the above is equivalent to:
MyObject o;
如果没有给出构造函数,则调用默认构造函数(即不带参数).
The default constructor (i.e. without parameters) is called if no constructor is given.
这篇关于C ++指针和对象实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文