首先看一段代码:
class Instrument
{
public:
virtual void play()const=0//非法的定义在抽象类中定义纯虚函数
{
cout<<"Instrument Play\n";
}
};
class Wind:public Instrument
{
void play( )const
{
cout<<"Wind Play\n";
}
};
void main()
{
Wind s;
Instrument &p = s;
p.play();
}
以下是纯虚函数在抽象类中的定义:
#include <iostream>
#include <string>
using namespace std;
class Instrument
{
public:
virtual void play()const =0;//纯虚函数 这个抽象类的虚函数指针是空的
//不可以内联实现纯虚函数,但是可以在类外部实现
};
//在基类作为抽象类的视乎为其的纯虚函数提供定义是可以的,这样可以使一些公共代码可以在一些或者所有的派生类中都能调用
void Instrument::play()const
{
cout<<"Instrument play()\n";
}
/*以下方式也可以
inline void Instrument::play()const
{
cout<<"Instrument play()\n";
}*/
class Wind:public Instrument
{
void play()const
{
Instrument::play();
cout<<"Wind Play()\n";
}
};
void main()
{
Wind s;
Instrument &p = s;
p.play();
}
下来要说的是要在使用一个纯虚析构函数的时候要提供定义:(这最好是一个准则,会在后边的避免很多错误)
#include <iostream>
#include <string>
using namespace std;
class pet
{
public:
virtual ~pet()=0;
};
pet::~pet()
{
cout<<"~pet()\n";
}
class Dog:public pet
{
public:
~Dog()
{
cout<<"~Dog()\n";
}
};
void main()
{
pet* p = new Dog;
delete p;
}
再看一个在构造函数和析构函数中调用虚函数:
#include <iostream>
#include <string>
using namespace std;
class base1
{
public:
base1()//构造函数左括号之前对象已经被编译器建立了,下面只是输出执行信息
{ f();//在构造函数中调用不管是不是虚构函数都是执行早绑定,因为此时类型建立的对象信息还不完全,无法进行动态绑定,所以调用的是本本地的函数
cout<<"base1::base()\n";
}
virtual void f()
{
cout<<"base1::f()\n";
}
virtual ~base1()
{
f();//这里对象的信息全但是此时还是会调用本地函数,因为编译器认为这些信息不可靠
cout<<"~base1()\n";
}
};
class Derived1:public base1
{
public:
Derived1()
{
cout<<"Derived1::Derived1()\n";
}
void f()
{
cout<<"Derived1::f()\n";
}
~Derived1()
{
cout<<"~Derived1()\n";
}
};
void main()
{
base1 *p=new Derived1;
delete p;
void*p = new Derived1;
delete p;//删除void*指针因为编译器不知道类型所以不会调用析构函数只是释放空间
}