虚函数的作用

1.虚函数可以用来实现动态函数绑定,子类和基类重载的函数在调用的时候可以对应各自对象的实现。

例如:

#include<iostream>
using namespace std;

class C
{
    public:
    C(){
        cout<<"C creator is using"<<endl;
        }
    virtual string toString(){
    return "this is C class";
    }
    virtual ~C(){
         cout<<"output memory of C"<<endl;
         }
};
class B:public  C
{
    public:
    B(){
        cout<<"B creator is using"<<endl;
        }
     string toString(){
    return "this is B class";
    }
    ~B(){
         cout<<"output memory of B"<<endl;
         }
    
};
class A:public  B{
    public:
    A(){
        cout<<"A creator is using"<<endl;
        }
    string toString(){
    return "this is A class";
    }
    ~A(){
         cout<<"output memory of A"<<endl;
         }
};

void display(C *x){
    cout<<x->toString().data()<<endl;
}
int main()
{
    C *c = new C();
    C *b = new B();
    C *a = new A();
    display(c);
    display(b);
    display(a);
    system("pause");
    return 0;
}
这样就可以分别结果是:


2.纯虚函数可以构建抽象类

3.当用基类声明的对象用子类实例化时(公有继承),那么基类的析构函数必须声明为虚函数,要不然会有内存泄露,子类占的内存并没有释放。

例如:

#include<iostream>
using namespace std;

class C
{
    public:
    
    C(){
        cout<<"C creator is using"<<endl;
        }

 virtual string toString(){ return "this is C class"; } ~C(){ cout<<"output memory of C"; }};class B:public C{ public:
   B(){
        cout<<"B creator is using"<<endl;
        }

 string toString(){ return "this is B class"; } ~B(){ cout<<"output memory of B"<<endl; } };class A:public B{ public:
    A(){
        cout<<"A creator is using"<<endl;
        }

  string toString(){ return "this is A class"; } ~A(){ cout<<"output memory of A"<<endl; }};void display(C *x){ cout<<x->toString().data()<<endl;}int main(){ C *c = new C(); C *b = new B(); C *a = new A(); display(c); display(b); display(a); delete a; system("pause"); return 0;}

delte a的时候结构显示只有基类的析构函数调用了,说明子类并没有从内存中完全释放。

在基类(A)的析构函数前加virtual之后,结果是:

这时对象a所占的内存才完全释放。

注意:这种函数的绑定只适合堆区动态分配的内存,如果只是在函数栈去声明的一个对象,虚函数并不能实现动态绑定。

例如:

int main()
{
    //C *c = new C();
    //C *b = new B();
    C *a = new A();
    C mc = A();
    //display(c);
    //display(b);
    display(a);
    display(&mc);
    delete a;
    system("pause");
    return 0;
}


具体原因是可以关注下一篇博客。。。

C++中,虚函数是一种特殊类型的成员函数,主要用于实现多态性,也就是通过基类类型的指针或引用调用派生类对象的成员函数。其作用具体体现在以下几个方面: - **允许派生类重写基类函数**:虚函数允许在派生类中重写基类的函数,从而实现特定于派生类的行为。当使用基类类型的指针或引用调用虚函数时,编译器会在运行时根据实际对象的类型来确定调用的是基类中的函数还是派生类中的函数[^1]。 - **实现动态多态**:虚函数C++中用于实现动态多态的成员函数。在基类中使用`virtual`关键字声明虚函数后,派生类能够重写(override)该函数。这样一来,当通过基类指针或引用调用此函数时,实际执行的将是派生类中的函数版本。例如以下代码: ```cpp #include <iostream> class Animal { public: virtual void makeSound() { std::cout << "Animal sound!" << std::endl; } }; class Dog : public Animal { public: void makeSound() override { std::cout << "Woof!" << std::endl; } }; int main() { Animal* animal = new Dog(); animal->makeSound(); delete animal; return 0; } ``` 在上述代码中,通过基类`Animal`的指针`animal`指向派生类`Dog`的对象,调用`makeSound()`函数时,实际执行的是`Dog`类中重写后的函数版本,输出“Woof!” [^3]。 - **防止内存泄漏**:当基类可能被继承时,必须将析构函数声明为虚函数,防止内存泄漏。因为如果基类的析构函数不是虚函数,当通过基类指针删除派生类对象时,只会调用基类的析构函数,而不会调用派生类的析构函数,从而导致派生类对象的部分资源无法正确释放,造成内存泄漏 [^4]。 - **实现代码的灵活性和可扩展性**:可以通过基类类型的指针或引用来访问派生类对象的特定成员函数,而无需知道实际对象的具体类型。这样可以实现代码的灵活性和可扩展性,使得程序可以根据需要动态决定调用哪个函数 [^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值