1
class Base {
public:
    virtual void myFunc(double a, double b) { };
    virtual void myFunc(double a) { };

};

class Derived : public Base {
public:
    virtual void myFunc(double a) { return this->myFunc(a, 0.0); };
};                                      // ^^^^^^^^^^^^^^^^^^^^

The previous code won't compile : error C2660: 'Derived::myFunc' : function does not take 2 arguments Apparently the compiler cannot see that I'm trying to call the function defined in Base class, or any function that overrides it. On the other hand, the following code compiles ok:

class Base {
public:
    virtual void myFunc2(double a, double b) { };
    virtual void myFunc(double a) { };

};

class Derived : public Base {
public:
    virtual void myFunc(double a) { return this->myFunc2(a, 0.0); };
};

I think what i'm trying to do in the first example is legal C++, so is this a bug in the VS2010 compiler? I have the same results with VS2008

thanks

edit : a workaround I found is to use

virtual void myFunc(double a) { return ((Base*)this)->myFunc(a, 0.0); };

but I'm not 100% sure it has the exact same effect, can anyone confirm?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
lezebulon
  • 7,607
  • 11
  • 42
  • 73
  • 3
    Try adding a `using Base::myFunc` to the beginning of your class `Derived`. – Kerrek SB May 09 '12 at 20:10
  • possible duplicate of [Why does an overridden function in the derived class hide other overloads of the base class?](http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the) – Tony May 09 '12 at 20:29

2 Answers2

7

This behavior is by design.

Functions in a derived class hide other overloads in the base class.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • So this would not compile on anything else? I'm not sure I see the point of this limitation – lezebulon May 09 '12 at 20:12
  • @lezebulon: Correct. I'm not sure why this is true. – SLaks May 09 '12 at 20:13
  • Can you be more specific? I would expect both overloads to be candidate functions, only the two-parameter one a viable function, and it would consequently be the best viable function. What am I missing? – Jirka Hanika May 09 '12 at 20:14
  • @JirkaHanika: All of the base-class overloads are hidden because of the derived-class functions. – SLaks May 09 '12 at 20:14
  • I will venture a partial explanation. Suppose that `Base` is a library class whereas `Derived` is your application code. The next version of the library will certainly have new overloads for many functions, possibly even `myFunc`, and you certainly do not want your well tested program to silent rebind its `myFunc` calls to other overloads and call something else. (Note similarly that C++ ignores access control (private/protected/public) during overload resolution, so that making a private member public or vice versa does not silently and subtly change a well formed program's semantics.) – Jirka Hanika May 09 '12 at 20:29
0

This is not a bug.

You have a choice in C++ whether the inherited overloads should be hidden (which is the safer, default behavior) or available:

class Derived : public Base {
public:
    virtual void myFunc(double a) { return this->myFunc(a, 0.0); };
    using Base::myFunc;
};

Alternatively, you can use the Base::myFunc syntax at the point of the function call.

There is an explanation of the default behavior of interaction between namespaces and overloading in D&E, section 17.4.5.3 and 17.5. Suppose that Base is a library class whereas Derived is your application code. The next version of the library will certainly have new overloads for many functions, possibly even myFunc, and you certainly do not want your well tested program to silent rebind its myFunc calls to other overloads and call something else.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75