theHoatzin
theHoatzin

Reputation: 127

How can I call parent class overloaded function from derived class instance?

I have a base class with an implemented function and a pure virtual function which share a name, but not arguments. I can't call the function implemented in the base class from an instance of the derived class.

I've tried adding the function to the derived class instead of the base class and it works, but that would mean I have to add the function to every derived class instead of implementing it once in the base class. I've also tried changing the functions name and it works. It seems to be due to a collision between the two overloaded functions.

template <typename T>
class Base {
public:
    virtual T some_function(int x) = 0;
    T some_function() {
    // some code
    }
}

class Derived final: public Base<int> {
    int some_function() {
    // some code
    }
}

int main() {
    Derived var = Derived();
    var.some_function(3);// Compilation error
} 

The above code does not compile indicating that it doesn't find the function: some_function(int).

Upvotes: 2

Views: 56

Answers (1)

The name some_function in Derived hides the name some_function inherited from Base. You have to bring it back into scope with a using declaration:

class Derived final: public Base<int> {
public:
    using Base<int>::some_function;
    int some_function() {
    // some code
    }
}

Upvotes: 5

Related Questions