C++,继承类中析构函数问题
#include<iostream>usingnamespacestd;classCBase{public:CBase(inti){m_data=i;cout<<"Con...
#include <iostream>
using namespace std;
class CBase {
public:
CBase(int i){ m_data = i;cout << "Constructor of CBase. m_data=" << m_data <<endl; }
~CBase(){ cout << "Destructor of CBase. m_data=" << m_data <<endl; }
protected:
int m_data;
};
class CDerived: public CBase
{
public:
CDerived(const char *s): CBase(strlen(s)){ m_data = new char[strlen(s) + 1];
strcpy(m_data, s);cout << "Constructor of CDerived. m_data = " << m_data << endl; }
~CDerived(){ delete m_data;cout << "Destructor of CDerived. m_data = " << m_data <<endl;}
private:
char *m_data;
};
int main()
{
CBase *p ;
p = new CDerived ("abcd");
delete p;
return 0;
}
此过程中为什么不执行子类的析构函数呢? 展开
using namespace std;
class CBase {
public:
CBase(int i){ m_data = i;cout << "Constructor of CBase. m_data=" << m_data <<endl; }
~CBase(){ cout << "Destructor of CBase. m_data=" << m_data <<endl; }
protected:
int m_data;
};
class CDerived: public CBase
{
public:
CDerived(const char *s): CBase(strlen(s)){ m_data = new char[strlen(s) + 1];
strcpy(m_data, s);cout << "Constructor of CDerived. m_data = " << m_data << endl; }
~CDerived(){ delete m_data;cout << "Destructor of CDerived. m_data = " << m_data <<endl;}
private:
char *m_data;
};
int main()
{
CBase *p ;
p = new CDerived ("abcd");
delete p;
return 0;
}
此过程中为什么不执行子类的析构函数呢? 展开
2个回答
推荐于2016-09-21
展开全部
因为基类的析构函数不是虚析构函数,所以是根据指针的类型调用析构函数,而不是根据指针指向对象的类型调用析构函数。
虚函数是在基类中被声明为 virtual,并在派生类中重新定义的成员函数,可实现成员函数的动态重载。
当一个类作为基类时,它的析构函数应该为虚析构函数。
虚函数是在基类中被声明为 virtual,并在派生类中重新定义的成员函数,可实现成员函数的动态重载。
当一个类作为基类时,它的析构函数应该为虚析构函数。
展开全部
解析函数要 virtual
#include <iostream>
using namespace std;
class CBase {
public:
CBase(int i){ m_data = i;cout << "Constructor of CBase. m_data=" << m_data <<endl; }
virtual ~CBase(){ cout << "Destructor of CBase. m_data=" << m_data <<endl; }
^^^^^^^^注意
protected:
int m_data;
};
class CDerived: public CBase
{
public:
CDerived(const char *s): CBase(strlen(s)){ m_data = new char[strlen(s) + 1];
strcpy(m_data, s);cout << "Constructor of CDerived. m_data = " << m_data << endl; }
virtual ~CDerived(){ delete m_data;cout << "Destructor of CDerived. m_data = " << m_data <<endl;}
^^^^^^^^注意
private:
char *m_data;
};
int main()
{
CBase *p ;
p = new CDerived ("abcd");
delete p;
return 0;
}
#include <iostream>
using namespace std;
class CBase {
public:
CBase(int i){ m_data = i;cout << "Constructor of CBase. m_data=" << m_data <<endl; }
virtual ~CBase(){ cout << "Destructor of CBase. m_data=" << m_data <<endl; }
^^^^^^^^注意
protected:
int m_data;
};
class CDerived: public CBase
{
public:
CDerived(const char *s): CBase(strlen(s)){ m_data = new char[strlen(s) + 1];
strcpy(m_data, s);cout << "Constructor of CDerived. m_data = " << m_data << endl; }
virtual ~CDerived(){ delete m_data;cout << "Destructor of CDerived. m_data = " << m_data <<endl;}
^^^^^^^^注意
private:
char *m_data;
};
int main()
{
CBase *p ;
p = new CDerived ("abcd");
delete p;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询