简体   繁体   English

C ++继承纯虚函数

[英]C++ Inheritance pure virtual functions

my problem is: How do I implement pure virtual functions in an inherited class? 我的问题是:如何在继承的类中实现纯虚函数? It always says i didn't implement the only function, but I tried to do it. 它总是说我没有实现唯一的功能,但是我试图做到这一点。 So where is my mistake? 那我的错误在哪里?

My code: 我的代码:

Ah: 啊:

class A {
public:
    A();
    virtual std::pair<A*, A*> f1(const A& o) const=0;
    virtual ~A();
};

Bh: Bh:

#include "A.h"

class B : public A {
public:
B();
virtual ~B();
virtual std::pair<A*, A*> f1(const A& o);
};

B.cpp: B.cpp:

#include "B.h"

B::B() : A() {}
B::~B() {}
std::pair<A*, A*> B::f1(const A& o) {
   A* K1=new B();
   A* K2=new B();
   return std::make_pair (K1, K2);
}

I get following error: 我收到以下错误:

B.cpp: In member function ‘virtual std::pair<A*, A*> B::f1(const A&)’:
B.cpp:14:16: error: cannot allocate an object of abstract type ‘B’
    A* K1=new B();
                ^
In file included from B.cpp:1:0:
B.h:4:7: note:   because the following virtual functions are pure within ‘B’:
 class B : public A {
       ^
In file included from B.h:1:0,
                 from B.cpp:1:
A.h:10:28: note:    virtual std::pair<A*, A*> A::f1(const A&) const
  virtual std::pair<A*, A*> f1(const A& o) const=0;
                            ^
B.cpp:15:16: error: cannot allocate an object of abstract type ‘B’
    A* K2=new B();
                ^
In file included from B.cpp:1:0:
B.h:4:7: note:   since type ‘B’ has pure virtual functions
 class B : public A {
       ^

Also: What is correct, A* K1=new A(); 另外:什么是正确的,A * K1 = new A(); or new B(); 或新的B(); ?

Your override has to match in cv-qualification. 您的替代必须在简历中匹配。 You're missing a const here: 您在这里缺少const

std::pair<A*, A*> B::f1(const A& o) /* [const] */

Since it doesn't override and your base class method is pure virtual, your derived class becomes abstract. 由于它不会覆盖,并且您的基类方法是纯虚拟的,因此派生类将成为抽象的。 You can't instantiate objects of abstract type. 您不能实例化抽象类型的对象。

You have to add const to both the declaration and definition. 您必须在声明和定义中都添加const Also, to make sure it overrides, use the keyword override : 另外,要确保它覆盖,请使用关键字override

std::pair<A*, A*> f1(const A& o) override; // error since it does not override

Lets' put those two side by side and look closer: 让我们将这两个并排放置并仔细观察:

A: virtual std::pair<A*, A*> f1(const A& o) const=0; 
B: virtual std::pair<A*, A*> f1(const A& o);

Well, I see a difference, one of these is a const function, and the other is not. 好吧,我看到了一个区别,其中一个是const函数,另一个不是。 That makes them two different functions. 这使它们具有两种不同的功能。 Since the const function was never overloaded, B is still abstract, like A , and cannot be instantiated. 由于const函数从未被重载,因此B仍然像A一样是抽象A ,并且无法实例化。

A* K1 = new A(); //would give you an A, if A weren't abstract.  Do you want an A or B?
A* K1 = new B(); //gives a B object, stored as a pointer to an A interface.

I also highly recommend using std::unique_ptr where you currently have owning raw pointers. 我还强烈建议您在当前拥有原始指针的地方使用std::unique_ptr They'll prevent headaches later. 他们会在以后防止头痛。

In B, your function needs to be virtual std::pair<A*, A*> f1(const A&) const; 在B中,您的函数需要为virtual std::pair<A*, A*> f1(const A&) const; or it's a different function, not overriding the one from A. 或这是一个不同的功能,而不是覆盖A中的功能。

(If you're using a C++11 compiler, do std::pair<A*, A*> f1(const A&) const override; and it becomes clear to the reader that you intended to override the function.) (如果使用的是C ++ 11编译器,请执行std::pair<A*, A*> f1(const A&) const override;并且读者可以清楚地了解要覆盖该函数。)

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

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