Question

In the following code:

#include <iostream>

class A
{
public:
    void f( float x ) { std::cout << 1; }
    void g() { std::cout << 11; }
};

class B : public A
{
public:
    void f( char x ) { std::cout << 2; }
    void g() { std::cout << 22; }
};

int main()
{
    B b;
    b.A::f( 0 );
    b.A::g();

    return 0;
}

Isn't this name hiding? And where is this syntax defined in The standard (C++11 or C++03, doesn't matter, it seems to be the same for both standards)?

I didn't know this is possible at all, that's the first time I see such syntax (saw it here for the first time: why cant i access class A function in following code? )

Était-ce utile?

La solution

yes it is name hiding. hence it is not overloading(and not overriding) . section 13.2 Declaration matching in N3485 explains about this.

13.2 Declaration matching

 1   Two function declarations of the same name refer to the same function if they are in
the same scope and have equivalent parameter declarations (13.1). A function member of
a derived class is not in the same scope as a function member of the same name in a base class. 

[ Example:


struct B {
int f(int);
};

struct D : B {
int f(const char*);
};
Here D::f(const char*) hides B::f(int) rather than overloading it.
void h(D* pd) {
pd->f(1); // error:
// D::f(const char*) hides B::f(int)
pd->B::f(1); // OK
pd->f("Ben"); // OK, calls D::f
}

--end example]

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top