我读到过当我们有指针成员和定义一个基类时,需要定义析构函数,但我不确定是否完全理解。其中一个让我不确定的是是否定义默认构造函数是无用的,因为我们总是默认获得一个默认构造函数。另外,我不确定我们是否需要定义默认构造函数来实现RAII原则(我们只需要在构造函数中放置资源分配而不定义任何析构函数吗?)。
class A
{
public:
~Account()
{
delete [] brandname;
delete b;
//do we need to define it?
};
something(){} =0; //virtual function (reason #1: base class)
private:
char *brandname; //c-style string, which is a pointer member (reason #2: has a pointer member)
B* b; //instance of class B, which is a pointer member (reason #2)
vector<B*> vec; //what about this?
}
class B: public A
{
public something()
{
cout << "nothing" << endl;
}
//in all other cases we don't need to define the destructor, nor declare it?
}