C++ inheritance and function overriding

32,264

Solution 1

The term used to describe this is "hiding", rather than "overriding". A member of a derived class will, by default, make any members of base classes with the same name inaccessible, whether or not they have the same signature. If you want to access the base class members, you can pull them into the derived class with a using declaration. In this case, add the following to class Y:

using X::spray;

Solution 2

That's so called 'hiding': Y::spray hides X::spray. Add using directive:

class Y : public X
{
public:
   using X::spray;
   // ...
};

Solution 3

Classes are scopes and a class scope is nested in its parent. You have exactly the same behavior with other nested scopes (namespaces, blocks).

What happen is that when the name lookup searches for the definition of a name, it looks in the current namespace, then in the englobing namespace and so on until it find one definition; the search then stop (that's without taking into account the complications introduced by argument dependent name lookup -- the part of the rules which allows to use a function defined in the namespace of one of its argument).

Share:
32,264
legends2k
Author by

legends2k

I roam around hunting for bits in mathematics, graphics, C++, C, Lua, Python and design-related questions; have learnt a lot here. I like giving elaborate answers with examples and citations. Here are some I enjoyed answering and asking. Answers: Rotating a Vector in 3D space Calculating a perpendicular offset from a diagonal line Deriving OpenGL's utility function gluLookAt How can a program be OS-independent? Efficiently calculating rotation matrix from 2 vectors In OpenGL why give gluPerspective before gluLookAt? How are textures referenced in shader? How many pixels is a meter? Rendering the HSV wheel How to interpolate hue values in HSV colour space? Shift operator in C Passing 2D array to a function Lua multiple assignment with tables Difference between move and forward Taking std::function with templatized parameter as a parameter Linkage of symbols within anonymous namespace within a regular namespace Confusion on strings in C Questions: Purpose of Unions Why is (void)0 a no-op in C and C++? When is a conditional variable needed, isn't a mutex enough? Mixing extern and const I can type 77 words per minute :)

Updated on July 09, 2022

Comments

  • legends2k
    legends2k almost 2 years

    In C++, will a member function of a base class be overridden by its derived class function of the same name, even if its prototype (parameters' count, type and constness) is different? I guess this a silly question, since many websites says that the function prototype should be the same for that to happen; but why doesn't the below code compile? It's a very simple case of inheritance, I believe.

    #include <iostream>
    using std::cout;
    using std::endl;
    
    class A {};
    class B {};
    
    class X
    {
    public:
        void spray(A&)
        {
            cout << "Class A" << endl;
        }
    };
    
    class Y : public X
    {
    public:
        void spray(B&)
        {
            cout << "Class B" << endl;
        }
    };
    
    int main()
    {
        A a;
        B b;
        Y y;
    
        y.spray(a);
        y.spray(b);
    
        return 0;
    }
    

    GCC throws

    error: no matching function for call to `Y::spray(A&)'
    note: candidates are: void Y::spray(B&)
    
  • legends2k
    legends2k over 14 years
    Thanks for the explanation, that made it clear. It works when I add the using directive here. But when I change the spray signatures to spray(int) in X and spray(float) in Y and call y.spray(1); and y.spray(1.0f); it works without the using directive. How come?
  • legends2k
    legends2k over 14 years
    Wow sorry, my bad, it still is seeing only the spray(float) of Y's, when I do y.spray(1); it type casts the int to float implicitly.
  • legends2k
    legends2k over 14 years
    Thanks for explaining the underlying reason behind the hinding; it really says why I got that error in the first place and why the using directive resolved it.
  • Wug
    Wug almost 11 years
    This is one of those answers that deserves its own special question, crafted with lots of search powder so people can find it better.