简体   繁体   English

具有继承和C ++的奇怪行为

[英]Odd behavior with inheritance and c++

So I am getting some weird behavior with some class hierarchy I am trying to make.I am implementing graphs and I am doing it by making a Graph class that is will be implemented by AdjacencyMatrixGraph and AdjacencyListGraph so they could be used as a Graph by anything that wanted to use them. 所以我在尝试创建的类层次结构中出现了一些奇怪的行为。我正在实现图,并通过创建一个Graph类来实现,该类将由AdjacencyMatrixGraph和AdjacencyListGraph实现,因此它们可以被任何东西用作Graph想要使用它们。

I have one pure virtual function in Graph that get's overwritten by the function in AdjacencyMatrixGraph, however I have a non virtual function of the same name, but different signature in Graph. 我在Graph中有一个纯虚函数,被AdjacencyMatrixGraph中的函数覆盖,但是我在Graph中有一个同名的非虚函数。 I cannot call the non virtual method of the Graph class when accessing an AdjacencyMatrix class, but when I rename the non virtual method it works fine. 访问AdjacencyMatrix类时,无法调用Graph类的非虚拟方法,但是当我重命名非虚拟方法时,它可以正常工作。

Like this: 像这样:

When the classes look like this 当类看起来像这样

class Graph
{
public:
   virtual void addVertex(Vertex vert, bool bidirectional)=0;
   void addVertex(unsigned int from, unsigned int to, double weight, bool bidirectional)
}

class AdjacencyMatrixGraph : public Graph
{
...
}




AdjacencyMatrixGraph test;
Vertex vert;
test.addVertex(vert,false);   //this statement compiles and works fine
test.addVertex(0,0,10.f,false)  //this statement fails to compile and says cadidates are addVertex(Vertex, bool)

However if I rename the non virtual method like so 但是,如果我这样重命名非虚拟方法

class Graph
{
public:
   virtual void addVertex(Vertex vert, bool bidirectional)=0;
   void addVert(unsigned int from, unsigned int to, double weight, bool bidirectional)
}

AdjacencyMatrixGraph test;
Vertex vert;
test.addVertex(vert,false);   //this statement compiles and works fine
test.addVert(0,0,10.f,false)  //this statement compiles and works fine

This makes no sense to me because I thought the compiler sees addVertex(Vertex, bool) and addVertex(unsigned int,unsigned int, double, bool) as two completely different symbols. 这对我来说毫无意义,因为我认为编译器将addVertex(Vertex,bool)和addVertex(unsigned int,unsigned int,double,bool)视为两个完全不同的符号。 So one shouldn't be overridden with inheritance, even if it would it shouldn't be possible because the symbols take different arguments. 因此,不应使用继承来覆盖它,即使由于符号采用不同的参数也不可能这样做。

The definition in a derived class hides the base class overload declarations. 派生类中的定义隐藏了基类重载声明。

To bring those into the scope of the derived class, use a using declaration, like 要将它们带入派生类的范围,请使用using声明,例如

using Graph::addVertex;

in the derived class. 在派生类中。

By the way, this is a FAQ . 顺便说一句,这是一个常见问题解答 It's often a good idea to check the FAQ before asking. 在问问之前检查FAQ通常是个好主意。 Or even just in general. 甚至只是一般而言。 :-) :-)

In this case AdjacencyMatrixGraph hides Graph::addVertex(unsigned int from, unsigned int to, double weight, bool bidirectional) . 在这种情况下, AdjacencyMatrixGraph隐藏了Graph::addVertex(unsigned int from, unsigned int to, double weight, bool bidirectional) To bring the function into scope use using declaration as shown below: 为了使功能到使用范围using声明如下图所示:

class A
{
public:
virtual void foo(int) = 0;
virtual void foo(std::string) { std::cout << "foo(string)" << std::endl; }
};

class B : public A
{
public:
using A::foo; //this unhides A::foo(std::string)
virtual void foo(int) { std::cout << "foo(int)" << std::endl; }
};

int main()
{
B b;
b.foo(1);
b.foo("hello");
}

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

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