overloaded functions are hidden in derived class
TTBOMK this doesn't have a real technical reason, it's just that Stroustrup, when creating the language, considered this to be the better default. (In this it's similar to the rule that rvalues do not implicitly bind to non-const
references.)
You can easily work around it be explicitly bringing base class versions into the derived class' scope:
class base {
public:
void f(int);
void g(int);
};
class derived : public base {
public:
using base::f;
void f(float);
void g(float); // hides base::g
};
or by calling the explicitly:
derived d;
d.base::g(42); // explicitly call base class version
Overriding overloaded methods hides some of the overloads
your compiler is 100% right.
you overloaded your function to take an integer as argument, then this function hid all of the base class function - so obj calls myMethod(int)
be default , yet you don't provide your function an integer.
if you fix your code to be obj.myMethod(4);
the problem solved.
when overriding a function in the derived class - it hides all the other base class overloads. one can still call the base class with this syntax :obj.Base::myMethod();
In more in depth answer , this is WHY it happens.
first of all, we need to understand HOW the compiler compiles Object oriented code. remember - the functions compiles into assembly commands and they sit in the code segment
. the variables compiles into rows of memory that sit wither in the stack , heap or data segments. functions sits in on part of the application , the variables in a complete different areas. how does the compiler compile a class with variables AND functions? well, it doesn't. the idea goes like this:
let's say a have a class named X with some variables and some functions
1) take all the member functions and bring them out of the class
2) add another argument to each-now-globally-declared functions - const X* this
3) each time you see the syntax x.someFunc(args..)
change it to be someFunc(args..,&x)
4) in the functions - if you don't recognize a symbol - try attaching it a this->
and see if you can compile this
5) compile X as C struct and the other as C functions
6)do the same for derived classes
(of course , there is the virtual table issue , but let's stop here)
IN YOUR EXAMPLE:
the psuedo code that might represent the compiler parsed-code is
struct Base{}
struct Derived{}
void myMethod(const Base* this);
void myMethod(int x,const Base* this);
void myMethod(int x, const Derived* this);
//what you tried to do is :
myMethod (&obj);
but the compiler can't find any function that matches these arguments!
this is not very intuitive for someone who don't initially knows how object oriented compiles, but it makes more sense after understanding this compiling procedure.
Why does an overridden function in the derived class hide other overloads of the base class?
Judging by the wording of your question (you used the word "hide"), you already know what is going on here. The phenomenon is called "name hiding". For some reason, every time someone asks a question about why name hiding happens, people who respond either say that this called "name hiding" and explain how it works (which you probably already know), or explain how to override it (which you never asked about), but nobody seems to care to address the actual "why" question.
The decision, the rationale behind the name hiding, i.e. why it actually was designed into C++, is to avoid certain counter-intuitive, unforeseen and potentially dangerous behavior that might take place if the inherited set of overloaded functions were allowed to mix with the current set of overloads in the given class. You probably know that in C++ overload resolution works by choosing the best function from the set of candidates. This is done by matching the types of arguments to the types of parameters. The matching rules could be complicated at times, and often lead to results that might be perceived as illogical by an unprepared user. Adding new functions to a set of previously existing ones might result in a rather drastic shift in overload resolution results.
For example, let's say the base class B
has a member function foo
that takes a parameter of type void *
, and all calls to foo(NULL)
are resolved to B::foo(void *)
. Let's say there's no name hiding and this B::foo(void *)
is visible in many different classes descending from B
. However, let's say in some [indirect, remote] descendant D
of class B
a function foo(int)
is defined. Now, without name hiding D
has both foo(void *)
and foo(int)
visible and participating in overload resolution. Which function will the calls to foo(NULL)
resolve to, if made through an object of type D
? They will resolve to D::foo(int)
, since int
is a better match for integral zero (i.e. NULL
) than any pointer type. So, throughout the hierarchy calls to foo(NULL)
resolve to one function, while in D
(and under) they suddenly resolve to another.
Another example is given in The Design and Evolution of C++, page 77:
class Base {
int x;
public:
virtual void copy(Base* p) { x = p-> x; }
};
class Derived : public Base{
int xx;
public:
virtual void copy(Derived* p) { xx = p->xx; Base::copy(p); }
};
void f(Base a, Derived b)
{
a.copy(&b); // ok: copy Base part of b
b.copy(&a); // error: copy(Base*) is hidden by copy(Derived*)
}
Without this rule, b's state would be partially updated, leading to slicing.
This behavior was deemed undesirable when the language was designed. As a better approach, it was decided to follow the "name hiding" specification, meaning that each class starts with a "clean sheet" with respect to each method name it declares. In order to override this behavior, an explicit action is required from the user: originally a redeclaration of inherited method(s) (currently deprecated), now an explicit use of using-declaration.
As you correctly observed in your original post (I'm referring to the "Not polymorphic" remark), this behavior might be seen as a violation of IS-A relationship between the classes. This is true, but apparently back then it was decided that in the end name hiding would prove to be a lesser evil.
Overloaded final function in derived class
This declaration in class B
void foo(int b) override
{
std::cout << b;
}
hides all other functions with the same name declared in class A (excluding of course the overriden function).
You can either call the function explicitly like this
b->A::foo();
Or include using declaration
using A::foo;
in the definition of class B.
Or you can cast the pointer to the base class pointer
static_cast<A *>( b )->foo();
Related Topics
Map Set/Get Requests into C++ Class/Structure Changes
How to Create a Shared Library with Cmake
Advantages of Pass-By-Value and Std::Move Over Pass-By-Reference
What Is the Most Elegant Way to Read a Text File with C++
What Exactly Does Stringstream Do
Should I Use Virtual, Override, or Both Keywords
How to Choose Between Map and Unordered_Map
How to Tell Where a Header File Is Included From
C++ - Passing References to Std::Shared_Ptr or Boost::Shared_Ptr
How to Use Doxygen to Create Uml Class Diagrams from C++ Source
Most Optimized Way of Concatenation in Strings
How to Programmatically Cause a Core Dump in C/C++
C++ Template Copy Constructor on Template Class
How to Store Functional Objects with Different Signatures in a Container
Constant Expression Initializer for Static Class Member of Type Double
What's the Fastest Way to Pack 32 0/1 Values into the Bits of a Single 32-Bit Variable