开发者

Overloading a method in a subclass in C++

开发者 https://www.devze.com 2022-12-11 05:33 出处:网络
Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base {

Suppose I have some code like this:

class Base {
    public:
      virtual int Foo(int) = 0;
};

class Derived : public Base {
    public:
      int Foo(int);
      virtual double Foo(double) = 0;
};

class Concrete : public Derived {
    public:          
      double Foo(double);
};

If I have a object of type Concrete, why can I not call Foo(int)?

If I change the name of Foo(double) so that it isn't o开发者_高级运维verloading Foo, then all is well and both methods are accessible, but this isn't what I want.

Similarly, if I remove Concrete class and implement Foo(double) in Derived, then both are accessible, but again, not what I want.


Name lookup happens before overload resolution, so once Foo has been found in Concrete, base classes won't be search for other methods called Foo. int Foo(int) in Derived is hidden by the Foo in Concrete.

You have a number of options.

Change the call to be explicit.

concrete.Derived::Foo(an_int);

Add a using declaration to Concrete.

class Concrete : public Derived {
public:          
   using Derived::Foo;
   double Foo(double);
};

Call the function through a base reference.

Derived& dref = concrete;
dref.Foo(an_int);


Foo(double) hides the function from your base. You can make it visible though:

class Concrete : public Derived 
{
public:          
  using Derived::Foo;
  double Foo(double);
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号