虚函数是在 C++ 中实现运行时多态的唯一方法吗? [英] Are virtual functions the only way to achieve Runtime Polymorphism in C++?

查看:62
本文介绍了虚函数是在 C++ 中实现运行时多态的唯一方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友问我如何在 C++ 中实现运行时多态?"我回答通过继承"

他说不行,只能用虚函数来实现".

所以我给了他一个下面代码的例子:-

#include使用命名空间标准;A级{上市:国际我;A(){i=100;}};B类:公共A{上市:国际j;B(){i = -1;j = 99;}};void func(A& myA){cout<<myA.i<<结束;}int main(){乙乙;A* a = 新 B();功能(*一);函数(b);删除一个;返回0;}

这里,函数 func() 引用了 A,但我们传递了 B 的对象,我们可以打印公共成员i"的值.他说这是编译时多态性.

我的问题是:-

1) 运行时多态是否只能通过虚函数实现?

2) 上面的例子是运行时多态还是编译时多态?

3) 如果我有以下代码:-

void func2(A& myA){cout<<我的A.i<<结束;//动态/静态将 myA 转换为 myBcout<<myB.j<<结束;}

它是什么样的多态性?或者甚至是多态?

解决方案

该示例未显示动态多态性.要调用的方法在编译时是已知的.对于应该调用哪个方法,没有运行时决定(基于实际对象类型).不同的类型没有不同的行为.

以动态多态为例.
您需要在 Base 类中提供一个 virtual 成员函数并在派生类中覆盖它.实际调用的方法由基类指针指向的对象的实际类型决定.

在线示例:

#include使用命名空间标准;A级{上市:虚空 doSomething(){std::cout<<"\nIn A::doSomething()";}};B类:公共A{上市:虚空 doSomething(){std::cout<<"\nIn B::doSomething()";}};int main(){乙乙;一个对象;A* a = &b;a->doSomething();a = &obj;a->doSomething();返回0;}

输出:

在 B::doSomething() 中在 A::doSomething() 中

<小时><块引用>

运行时多态性是否只能通过虚拟函数实现?

不,但是虚拟函数是最常见和最正确的方法.
多态可以通过函数指针来实现.考虑下面的代码示例,实际调用的方法是在运行时决定的在用户输入上.它是一种多态形式,并非严格意义上的 C++,它为不同的类型强制要求不同的行为.

#include typedef void (*someFunction)(int, char*);void FirstsomeFunction(int i, char *c){std::cout<<"\n 在 FirstsomeFunction";}void SecondsomeFunction(int i, char *c){std::cout<<"\n 在 SecondsomeFunction";}int main(){someFunction arr[1];整数 x = 0;std::cin >>X;如果(x == 0)arr[0] = &FirstsomeFunction;别的arr[0] = &SecondsomeFunction;(arr[0])(10,"你好");返回0;}

<块引用>

上面的例子是运行时多态还是编译时多态?

没有任何类型的多态性.在所有情况下都将调用相同的方法.不同的类型没有不同的行为,因此它不属于任何类型的多态性.

One of my friends asked me "How Runtime Polymorphism is achieved in C++?" I answered "By Inheritance"

He said "No, it can be achieved only using virtual functions".

So I gave him an example of the following code :-

#include<iostream>
using namespace std;

class A
{
public:
    int i;
    A(){i=100;}
};

class B : public A
{
public:
    int j;
    B(){i = -1; j = 99;}
};

void func(A& myA)
{
    cout<<myA.i << endl;
}

int main()
{
    B b;
    A* a = new B();
    func(*a);
    func(b);
    delete a;
    return 0;
}

Here, function func() takes reference of A but we pass object of B and we can print the value of public member "i". He said it is compile time polymorphism.

My questions are :-

1) Is Runtime polymorphism achieved only with virtual functions?

2) Is the example above has runtime polymorphism or compile time?

3) If I have the following code :-

void func2(A& myA)
{
    cout << myA.i << endl;
    // dynamic/static cast myA to myB
    cout<<myB.j << endl;
}

what kind of polymorphism is it? Or is it even polymorphism?

解决方案

The example does not show dynamic polymorphism. The method to be called is known at compile time. There is no runtime decision(based on actual object type) as to which method should be called. There is no different behavior for different types.

For the example to be example of dynamic polymorphism.
You need to provide a virtual member function in Base class and overide it in derived class. The actual method to be called is decided by the actual type of the object pointed by the Base class pointer.

Online sample:

#include<iostream>
using namespace std;

class A
{
public:
    virtual void doSomething()
    {
        std::cout<<"\nIn A::doSomething()";
    }
};

class B : public A
{
public:
    virtual void doSomething()
    {
        std::cout<<"\nIn B::doSomething()";
    }
};



int main()
{
    B b;
    A obj;
    A* a = &b;
    a->doSomething();

    a = &obj;
    a->doSomething();

    return 0;
}

Output:

In B::doSomething()
In A::doSomething()


Is Runtime polymorphism acheived only with virutal functions?

No, but virtual functions is the most common and correct way to do so.
Polymorphism can be achieved through function pointers. Consider the following code example, the actual method to call is decided at run-time depending on user input. It is a form of polymorphism through not in strict sense C++ sense which mandates different behaviors for different types.

#include <iostream>

typedef void (*someFunction)(int, char*);

void FirstsomeFunction(int i, char *c)
{
    std::cout<<"\n In FirstsomeFunction";
}

void SecondsomeFunction(int i, char *c)
{
    std::cout<<"\n In SecondsomeFunction";
}

int main()
{
    someFunction arr[1];
    int x = 0;
    std::cin >> x;

    if(x ==0)
        arr[0] = &FirstsomeFunction;
    else
        arr[0] = &SecondsomeFunction;

    (arr[0])(10,"Hello");

    return 0;
}

Is the example above has runtime polymorphism or compile time?

There is no polymorphism of any kind. The same method will be called in all cases. There is no different behavior for different types and hence it does not classify as polymorphism of any kind.

这篇关于虚函数是在 C++ 中实现运行时多态的唯一方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆