C++: Initialize a member pointer to null?

2019-02-05 12:30发布

I have a class that looks like:

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

private:
    Odp* bar;
};

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

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?)

7条回答
女痞
2楼-- · 2019-02-05 13:22
  1. Yes
  2. Regarding your second question about destructor being virtual see: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 Short answer, No destructor isn't necessary to be vitual in your case.
  3. There are no such things as virtual constructors.
查看更多
登录 后发表回答