且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++:初始化一个成员指针为null?

更新时间:2023-11-30 23:50:40


以初始化 bar NULL 。这是***的方法吗?

I wish to initialize bar to NULL. Is this the best way to do it?

这是正确的方式。所以,是的。

It is the correct way. So, yes.


此外,析构函数是否必须是虚拟的?

Also, is it necessary that the destructor is virtual?

否。如果你将继承 Foo 类,并且将使用 Foo 指针,析构函数只需要是virtual删除这些派生类(虽然作为一般的经验法则,如果有任何其他虚拟成员,它应该是虚拟的)。

No. The destructor only needs to be virtual if you will be inheriting from the Foo class and will be using a Foo pointer to delete those derived classes (although as a general rule of thumb, it should be virtual if there are any other virtual members).


这是真的,那么构造函数是否也必须是虚拟的?)

(If that is true, then must the constructor be virtual as well?)

否。构造函数不需要 virtual ,也不能

No. Constructors neither need to be virtual, nor can they.