您的位置:首页 > 编程语言 > C语言/C++

带Body的纯虚函数与不带Body的虚函数?(C++学习)

2011-05-12 23:56 302 查看
虚函数

纯虚函数

基类本身可生成对象
基类本身不能生成对象
基类中必须提供实现
必须在派生类中提供实现
"实体继承"
"接口继承"

纯虚函数

带纯虚函数的类叫虚基类(也叫抽象类),这种基类不能直接生成对象,而只有被继承,才能使用。

有一点有意思:

纯虚函数也可以提供实现

首先想到的:如果纯虚函数有了实现,是不是说明了这个基类可以直接生成对象了?
class A
{
public:
virtual void fun()=0;
};

void A::fun()
{
printf("A::fun/n");
};

int main()
{
A * a = new A;
a->fun();
delete a;
return 0;
}

事实证明不行。g++与cl分别给出错误信息如下:
error: cannot allocate an object of abstract type 'A'
because the following virtual functions are pure within 'A':
virtual void A::fun()

 
error C2259: 'A' : cannot instantiate abstract class
due to following members:
'void A::fun(void)' : is abstract
see declaration of 'A::fun'

想想也是哈,如果这样的话,和虚函数就没有任何区别了。
然后闪现的是:是不是派生类可以不用为它提供实现了呢?
class B: public A
{
};
int main()
{
A * a = new B;
a->fun();
delete a;
return 0;
}

恩,出现和上面一样的编译错误。那么这个东西是怎么用的呢?
 
class B: public A
{
public:
void fun()
{
A::fun();
printf("B::fun/n");
}
};

原来需要这样被调用。
为纯虚函数添加定义体应该没什么必要,似乎对纯析构函数,某些时候有用?
ISO C++ 12.4 (7):
A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined. If a class has a base class with a virtual destructor, its destructor (whether user or implicitly declared) is virtual.

 
ISO C++ 10.4 (2):
A pure virtual function need be defined only if explicitly called with the qualified id syntax (5.1).

虚函数

纯虚函数可以有函数体,那么虚函数是不是可以没有函数体呢?
而且今天在newsmth看到一个网友问:为什么没有函数体不出错?
class A
{
public:
virtual void fun();
};

class B
{
public:
virtual void fun();
};

class C: public A, public B
{
public:
void fun()
{
printf("C::fun/n");
}
};

int main()
{
return 0;
}

我也不知道具体原因,只是感觉上:程序中根本就没使用它,应该不会出错。
甚至还在想,反正C的对象不需要使用A和B中的虚函数,这时创建C的对象应该也没问题吧?
int main(int argc, char* argv[])
{
C c;
c.fun();
return 0;
}

编译器直接告诉结果了:
cckOIw8K.o:virtual.cpp:(.text$_ZN1AC2Ev[A::A()]+0x8): undefined reference to `vtable for A'
cckOIw8K.o:virtual.cpp:(.text$_ZN1BC2Ev[B::B()]+0x8): undefined reference to `vtable for B'
collect2: ld returned 1 exit status

 
virtual.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall A::fun(void)" (?fun@A@@UAEXXZ)
virtual.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall B::fun(void)" (?fun@B@@UAEXXZ)
virtual.exe : fatal error LNK1120: 2 unresolved externals

好奇怪啊?为何纯虚函数就不需要,虚函数非要有定义呢(我们又不需要使用这个虚函数)。
看来需要学习一点C++对象的内存模型了

参考

http://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained

http://stackoverflow.com/questions/5481941/c-pure-virtual-function-have-body

http://www.codeguru.com/forum/showthread.php?t=356281

http://www.parashift.com/c++-faq-lite/virtual-functions.html

http://www.artima.com/cppsource/pure_virtual.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息