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

虚函数

纯虚函数

基类本身可生成对象

基类本身不能生成对象

基类中必须提供实现

必须在派生类中提供实现

"实体继承"

"接口继承"

纯虚函数

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

有一点有意思:

  • 纯虚函数也可以提供实现

首先想到的:如果纯虚函数有了实现,是不是说明了这个基类可以直接生成对象了?

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++对象的内存模型了

参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值