views:

341

answers:

2

Hi,

Is it possible to access a base class function which has the same signature as that of a derived class function using a derived class object?. here's a sample of what I'm stating below..

class base1 {
public:
    void test()
    {cout<<"base1"<<endl;};
};

class der1 : public base1 {
public:
    void test()
    {cout<<"der1"<<endl;};
};

int main() {
der1 obj;
obj.test(); // How can I access the base class 'test()' here??
return 0;
}
+6  A: 

You need to fully qualify the method name as it conflicts with the inherited one.

Use obj.base1::test()

Arkaitz Jimenez
The formal term is "overrides", since the signatures match. Otherwise you'd say the derived "hides" the base class method. In either case, there is no "inherited method name" anymore.
MSalters
I doubt whether the correct term is 'override' or 'hides'. In the C++ standard 'override' is only used with virtual functions and in 10.2 Member name lookup, the standard says: 'A member name f in one sub-object B hides a member name f in a sub-object A if A is a base class sub-object of B.'
David Rodríguez - dribeas
Thanks Arkaitz.
@MSalters: This is definitely not "overriding". That is only for virtual functions.
Richard Corden
A: 
shafeer