简体   繁体   English

从具有不同签名的基类访问隐藏函数

[英]Accessing hidden functions from base classes with different signatures

We have: 我们有:

class A {
public:
    int f();
    int f(int);
  //...
};
class B {
public:
    int f();
    int f(int);
  //...
};
class AB : public A, public B {
public:
    long f(double, double);
  //...
};

A::f() , A::f(int) , B::f() , B::f(int) are now hidden in class AB , and I want to use only A::f() and B::f(int) as if they weren't hidden: A::f()A::f(int)B::f()B::f(int)现在隐藏在class AB ,我只想使用A::f()B::f(int)好像它们没有被隐藏:

AB ab;
ab.f();  // ab.A::f()
ab.f(1); // ab.B::f(1)

Is there a simpler way to achieve this than by writing the following code? 有没有比编写以下代码更简单的方法来实现此目的?

class AB : public A, public B {
public:
  //...
    int f() {return A::f();}
    int f(int x) {return B::f(x);}
};

I thought of the using keyword but it doesn't distinguish between methods with the same name and different signatures. 我想到过using关键字,但是它不能区分具有相同名称和不同签名的方法。

The only way is to declare them and call the base class function that you want as you wrote. 唯一的方法是声明它们,并在编写时调用所需的基类函数。

You might have thought about using in some weird way "using" ex: using A::f ; 您可能已经考虑过以某种奇怪的方式使用“ using”例如: using A::f ; but you inherit all function signatures with the same and you cannot specify a single one. 但是您继承具有相同功能的所有函数签名,并且不能指定单个签名。

The thing in AB class declaration is: AB类声明中的内容是:

  • With just the inheritance 只是继承

You got the interface and implementation from both base class you will have a error when calling to a method that is common on both of them(Note!Even if the signature is not the same). 您从两个基类都获得了接口和实现,在调用这两个基类上都通用的方法时会出错(注意!即使签名不一样)。 theABVar.A::f(); will give you: 会给你:

Error:"request for member 'f' is ambiguous" 错误:“对成员'f'的请求不明确”

But you can solve this with theABVar.A::f(); 但是您可以使用theABVar.A::f();解决此问题theABVar.A::f(); (isn't this cool? :D) (这很酷吗?:D)

  • When you add a method with the same name that in both base classes 当您添加与两个基类同名的方法时

The members in the base classes got hidden, therefore, a call to the method is not ambiguous any more. 基类中的成员被隐藏了,因此,对该方法的调用不再歧义。
But to call method on base class you should do the same trick than before. 但是要在基类上调用方法,您应该执行与以前相同的技巧。

theABVar.f(); will give you 会给你

Ettor: no matching function for call to 'AB::f()' Ettor:没有匹配的函数可以调用“ AB :: f()”

  • When you try to use "using" for base class functions You will get on compilation time, even if you don't use the function: 当您尝试对基类函数使用“ using”时,即使您不使用该函数,也将获得编译时间:

    using A::f; 使用A :: f; using B::f; 使用B :: f;

using declaration 'using B::f' conflicts with a previous using declaration using声明'using B :: f'与先前的using声明冲突

Any way, think twice about what you are doing. 无论如何,请三思而后行。
You want a function, that depending on the number/type of arguments it calls to a member of a different base class?. 您需要一个函数,它根据参数的数量/类型调用不同基类的成员? I can not imagine a lot of situations where that makes sense. 我无法想象在很多情况下这是有道理的。

--------------Conclusion--------------- - - - - - - - 结论 - - - - - - - -

  1. Think twice about what you are doing. 请三思而后行。 Hiding methods is not usually good. 隐藏方法通常不好。
  2. In your first example Clients can use explicit calls(they will be able to access all public members) theABVar.A::f() , theABVar.A::f(3) , theABVar.B::f() , theABVar.B::f(3) , theABVar.AB::f(3.0,3.0) and theABVar.f(3.0,3.0) . 在第一个示例中,客户可以使用显式调用(他们将能够访问所有公共成员) theABVar.A::f()theABVar.A::f(3)theABVar.B::f()theABVar.B::f(3)theABVar.AB::f(3.0,3.0)theABVar.f(3.0,3.0)
  3. You can create the functions you need as you specified(there is no simpler thing), but remember that clients can still call you base functions! 您可以根据需要创建所需的功能(没有简单的事情),但是请记住,客户端仍然可以调用基本功能!

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

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