C++虚函数实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1679770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:53:02  来源:igfitidea点击:

C++ Virtual function implementation?

c++inheritancevirtual

提问by atp

If I have in C++:

如果我在 C++ 中有:

class A {
    private: virtual int myfunction(void) {return 1;}
}

class B: public A {
    private: virtual int myfunction(void) {return 2;}
}

Then if I remove virtualfrom the myfunctiondefinition in class B, does that mean that if I had a class Cbased on class B, that I couldn't override the myfunctionsince it would be statically compiled?

然后,如果我virtual从 中的myfunction定义中删除class B,这是否意味着如果我有一个class C基于class B,我不能覆盖 ,myfunction因为它将被静态编译?

Also, I'm confused as to what happens when you switch around public, and private here. If I change the definition of myfunctionin class Bto be public (and the one in class Aremains private), is this some sort of grave error that I shouldn't do? I think that virtual functions need to keep the same type so that's illegal, but please let know if that's wrong.

另外,我很困惑当你在这里切换公共和私人时会发生什么。如果我将myfunctionin的定义更改class B为公开(而 inclass A仍然是私有的),这是我不应该做的某种严重错误吗?我认为虚函数需要保持相同的类型,所以这是非法的,但如果这是错误的,请告诉我们。

Thanks!

谢谢!

回答by

The first definition with 'virtual' is the one that matters. That function from base is from then on virtual when derived from, which means you don't need 'virtual' for reimplemented virtual function calls. If a function signature in a base class is not virtual, but virtual in the derived classes, then the base class does not have polymorphic behaviour.

“虚拟”的第一个定义很重要。从那时起,来自 base 的函数在派生自时就在 virtual 上,这意味着您不需要“virtual”来重新实现虚拟函数调用。如果基类中的函数签名不是虚拟的,而是派生类中的虚拟函数,则基类不具有多态行为。

class Base
{
    public:
    void func(void){ printf("foo\n"); }
};
class Derived1 : public Base
{
    public:
    virtual void func(){ printf("bar\n"); }
};
class Derived2 : public Derived1
{
    public:
    /*  reimplement func(), no need for 'virtual' keyword
        because Derived1::func is already virtual */
    void func(){ printf("baz\n"); } 
};

int main()
{
    Base* b = new Derived1;
    Derived1* d = new Derived2;

    b->func(); //prints foo - not polymorphic
    d->func(); //prints baz - polymorphic
}

回答by Chris Bednarski

once a function is made virtual in a base class, it will be virtual for every other subclass.

一旦一个函数在基类中成为虚拟的,它对于其他所有子类都是虚拟的。

public, protected and private do not affect the virtual nature of functions.

public、protected 和 private 不影响函数的虚拟性质。

回答by bua

If You remove virtualfrom the myfunctiondefinition in class B,

如果你virtualmyfunctionB 类的定义中删除,

compiler will add this for You. To fill out V-Table for polymorphic types.

编译器会为你添加这个。为多态类型填写 V-Table。

!!BUT!!

!!但!!

You will only have access to public members of class A (class B: public A)

您将只能访问 A 类的公共成员(B 类:公共 A)

the definition :

定义 :

class B: private A
{

}

Will cause that all (even public) members of class A, will become private for class B. Simplifies You will not have an access to A public members.

将导致 A 类的所有(甚至公共)成员都将成为 B 类的私有成员。简化您将无法访问 A 公共成员。

To workaround You can declare some friend:

解决方法您可以声明一些朋友:

class A
{
    private:
        friend class B;
}

More great info HERE.

更多精彩信息在这里

回答by qid

The behavior of virtualis that it effects which method is called when you have a pointer of one type that points to an object of a subtype. For example:

的行为virtual是,当您有一个指向子类型对象的一种类型的指针时,它会影响调用哪个方法。例如:

B* obj = new B;
A* base = obj;

What happens when you call obj->myfunction()depends on whether Adeclares myfunctionto be virtual. If it is not virtual, the reasoning is: we have a pointer of type A, so we call the function defined in A, and the result is 1. If Adefines myfunctionto be virtual, however, then a look-up is performed at run-time based on the type of the actual object, rather than the type of the pointer; since the object is actually a B, the implementation defined in Bis used and the result is 2.

调用时会发生什么obj->myfunction()取决于是否A声明myfunction为虚拟。如果它不是虚拟的,原因是:我们有一个类型为 的指针A,所以我们调用 中定义的函数A,结果为 1。但是,如果A定义myfunction为虚拟,则在运行时执行查找基于实际对象的类型,而不是指针的类型;由于对象实际上是 a B,因此B使用中定义的实现,结果为 2。

Further information can be found in the C++ FAQ Lite section on virtual functions.

更多信息可以在关于虚函数的 C++ FAQ Lite 部分找到。