简体   繁体   English

c ++多态性和非虚函数

[英]c++ polymorphism and non virtual functions

I have a question: How can I implement a non_virtual function in class that derived from abstract class?我有一个问题:如何在从抽象类派生的类中实现非虚拟函数? For example:例如:

class A {
    public:
     virtual void foo() = 0;
};

class B : public A{
    public:
        virtual void foo() {
            /* some code */
        }
        //B function
        void Bfoo(){
            /* some code */
        }
};

int main(){
    A* a = new B;
    a -> foo();
    a -> Bfoo();
    return 0;
}

g++ gets: g++ 得到:

error: ‘class A’ has no member named ‘Bfoo’ 
a -> Bfoo();

Is there a way to use polymorphism and use non-virtual functions?有没有办法使用多态和使用非虚函数?

EDIT编辑

I know that A hasn't got Bfoo() function so I get the error.我知道 A 没有Bfoo()函数所以我得到了错误。 I asked this question because I didn't understand when is useful use polymorphism.我问这个问题是因为我不明白什么时候使用多态是有用的。

My answer could be:我的回答可能是:

I use polymorphism when my derived classes have all the same function and derived from one common base class.当我的派生类具有所有相同的功能并从一个公共基类派生时,我使用多态性。

But if a class has an other function the polymorphism drops?但是如果一个类有另一个函数,多态性会下降吗?

If you want to use function Bfoo() with pointer a , you have to add the function Bfoo() to class A as virtual .如果要将函数Bfoo()与指针a一起a ,则必须将函数Bfoo()作为virtual添加到class A中。

I mean我的意思是

class A {
    public:
    virtual void foo() = 0;
    virtual void Bfoo() {} //this line is necessary
};

EDIT (after question changed)编辑(问题更改后)

The knowledge of definitions is necessary.定义的知识是必要的。 Here is the definition of Virtual Members from cplusplus.com这是来自cplusplus.com的虚拟会员的定义

Virtual members虚拟会员
A virtual member is a member function that can be redefined in a derived class, while preserving its calling properties through references.虚拟成员是可以在派生类中重新定义的成员函数,同时通过引用保留其调用属性。 The syntax for a function to become virtual is to precede its declaration with the virtual keyword.使函数变为虚函数的语法是在其声明之前使用 virtual 关键字。

See the resource for definitions of Polymorphism and Virtual Functions/Members .有关多态性虚拟函数/成员的定义,请参阅资源

A derived class can have additional non-virtual functions, but they must be called from a pointer or reference to the derived class .派生类可以有额外的非虚函数,但它们必须从派生类的指针或引用中调用。

The point of polymorphism is for the calling code to not have to care about what concrete class the variable it's acting on is.多态性的要点是调用代码不必关心它所作用的变量是哪个具体类。

Usually such methods would be part of a class's private implementation, and not part of the public interface.通常这些方法是类私有实现的一部分,而不是公共接口的一部分。 Any non-virtual public facing methods in a derived class implicitly force the calling code to be aware of the concrete type of the class.派生类中的任何非虚拟面向公众的方法都隐式地强制调用代码知道类的具体类型。

You could of course cast your A* to a B* but this kind of defeats the point of polymorphism.你当然可以将你的A*转换为B*但这种方式违背了多态性的意义。 It's hard to give more specific advice without a concrete example of what you're trying to do.如果没有你想要做的具体例子,很难给出更具体的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM