C++:将成员指针初始化为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3197434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:16:49  来源:igfitidea点击:

C++: Initialize a member pointer to null?

c++oopinitialization-list

提问by Nick Heiner

I have a class that looks like:

我有一个看起来像的类:

class Foo
{
public:
    Foo();
    virtual ~Foo();

private:
    Odp* bar;
};

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

我希望初始化barNULL. 这是最好的方法吗?

Foo::Foo() : bar(NULL)
{
}

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

另外,析构函数是否必须是虚拟的?(如果这是真的,那么构造函数也必须是虚拟的吗?)

回答by greyfade

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

我希望初始化barNULL. 这是最好的方法吗?

It is the correctway. So, yes.

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

Also, is it necessary that the destructor is virtual?

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

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

不。如果您将从Foo类继承并使用Foo指针删除那些派生类,析构函数只需要是虚拟的(尽管作为一般经验法则,如果有任何其他虚拟成员,它应该是虚拟的)。

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

(如果这是真的,那么构造函数也必须是虚拟的吗?)

No. Constructors neither needto be virtual, nor canthey.

号构造函数既不需要virtual,也可以他们。

回答by Tyler McHenry

  1. Yes, the initializer list is best.

  2. Maybe. The destructor should be virtual if you intend to have any other virtual functions in the class, or if you intend the class to be inherited from (although usually those things go together).

  3. No. It's not possible to have a virtual constructor in C++. (what would such a thing even mean?)

  1. 是的,初始化列表是最好的。

  2. 也许。如果您打算在类中拥有任何其他虚函数,或者您打算从该类继承(尽管通常这些东西一起出现),那么析构函数应该是虚拟的。

  3. 不。在 C++ 中不可能有虚拟构造函数。(这样的事情甚至意味着什么?)

The nature of your question suggests to me that you don't really understand what the virtualkeyword does, or what is is for, and you are just copying something you saw elsewhere or in a tutorial. It's best to understand the purpose of allof the code you're writing. Here might be a place to start: http://www.parashift.com/c++-faq-lite/virtual-functions.html

您的问题的性质告诉我,您并不真正了解virtual关键字的作用或用途,而您只是在复制您在其他地方或教程中看到的内容。最好了解您正在编写的所有代码的目的。这里可能是一个开始的地方:http: //www.parashift.com/c++-faq-lite/virtual-functions.html

回答by Johannes Schaub - litb

Four distinct ways exist. Which one is the best is up to you

存在四种不同的方式。哪一个是最好的取决于你

Foo::Foo() : bar() // value initialization
{
}

Foo::Foo() : bar(0) // direct null pointer constant
{
}

Foo::Foo() : bar(NULL) // null pointer constant by macro
{
}

Foo::Foo() : bar(nullptr) // pointer literal of type std::nullptr_t
{
}

回答by bits

  1. Yes
  2. Regarding your second question about destructor being virtual see: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7Short answer, No destructor isn't necessary to be vitual in your case.
  3. There are no such things as virtual constructors.
  1. 是的
  2. 关于您关于析构函数是虚拟的第二个问题,请参阅:http: //www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7简短回答,在您的情况下,没有必要使用析构函数.
  3. 没有虚拟构造函数之类的东西。

回答by Michael Burr

Another option you might want to consider is to use a smart pointer class (such as boost::scoped_ptr, boost::shared_ptror C++0x's unique_ptr) instead of a raw pointer. The constructor of the smart pointer will make sure it's initialized to something NULL-like if you don't need some other explicit initialization. The smart pointer will also ensure that the pointed-to object is destroyed.

您可能要考虑的另一个选项是使用智能指针类(例如boost::scoped_ptr,boost::shared_ptr或 C++0x's unique_ptr)而不是原始指针。如果您不需要一些其他显式初始化,智能指针的构造函数将确保它被初始化为类似 NULL 的东西。智能指针还将确保所指向的对象被销毁。

You just need to decide what kind of smart point policy is appropriate for the item and choose accordingly (even auto_ptrmight be better than a raw pointer as long as you're aware of the various pitfalls).

您只需要决定哪种智能点策略适合该项目并进行相应选择(auto_ptr只要您了解各种陷阱,甚至可能比原始指针更好)。

回答by Martin Beckett

1, yes

1,是的

2, only if you want somebody to be able to derive from your class and use a pointer to the base class - but make the dtor virtual anyway

2,仅当您希望某人能够从您的类派生并使用指向基类的指针时 - 但无论如何都要使 dtor 成为虚拟的

3, no you can't have a virtual ctor (or all ctors are virtual I suppose?)

3,不,你不能有一个虚拟的 ctor(或者我想所有的 ctors 都是虚拟的?)

回答by Priji

Virtual functions are to determine which function of class (which is defined in both base and derived class) must be called during run time. But when object is created compiler knows which constructor is to be called. for eg. when base object is created base constructor is calledand same for the derived class. Hence making constructor to be virtual doesnt makes any sense.But once when the base class object pointer points to the derived class object, and then destructor is called ,compiler gets confused which destructor(either of baseor derived) needs to be called, which is can only resolved using lookup table vtable and hence destructor needs to be virtual.

虚函数用于确定在运行时必须调用类的哪个函数(在基类和派生类中都定义)。但是当创建对象时,编译器知道要调用哪个构造函数。例如。当基对象被创建时,基构造函数被调用,对于派生类也是如此。因此使构造函数为虚拟没有任何意义。但是一旦基类对象指针指向派生类对象,然后调用析构函数,编译器就会混淆需要调用哪个析构函数(基类或派生的),这是只能使用查找表 vtable 解决,因此析构函数需要是虚拟的。