mercredi 23 août 2017

C++ template: Why do I have to add full qualification to access overloaded base class function? [duplicate]

This question already has an answer here:

When I compile this code, the compiler complains "invalid conversion from ‘const char*’ to ‘int’".

#include <iostream>

using namespace std;
template<int a>
struct A{
    void f(char* x){
        cout<<a<<' '<<x<<endl;
    }
};

template<int b>
struct B:public A<b-1>{
    void f(int y){
        this->f("xxx"); //Error: cannot convert from 'const char*' to 'int'
        cout<<b<<' '<<y<<endl;
    }
};

int main(){
    B<10> obj;
    obj.f(3);
}

Why can't I access overloaded f(char*) in the base class? When I change the name of the base function to g(char*), it compiles with no problem at all.

For a reason I don't want to write this->A<b-1>::f("xxx"). Is there some workaround?

Aucun commentaire:

Enregistrer un commentaire