This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 9 years ago.
In the following code, "d.Foo()" throws a compiler error claiming that function Foo() does not take 0 arguments. Yet a 0-argument function with that name exists in the base class. The line "d.Base::Foo()" is acceptable.
I have a vague memory of learning that the use of a function name in a derived class hides all functions of that name in a base class, even though arguments may be different. I don't remember why, nor do I remember the best way to avoid that problem. Is my solution best, or is there another way to get at Base::Foo()?
Thanks very much!
RobR
// Override.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class Base
{
public :
void Foo()
{
}
};
class Derived : public Base
{
public:
void Foo(int x)
{
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived d;
d.Foo();
d.Base::Foo();
return 0;
}
you can use(!) base class member functions via using
class Derived : public Base {
public:
using Base::Foo;
};
You could define Derived::Foo() as:
class Derived : public Base {
public:
void Foo() { Base::Foo(); }
};
Related
recently i came to know this - if a derived class redefines base class member method(s) then all the base class methods with same name become hidden in derived class.
#include<iostream>
using namespace std;
class Base
{
public:
int fun()
{
cout<<"Base::fun() called";
}
int fun(int i)
{
cout<<"Base::fun(int i) called";
}
};
class Derived: public Base
{
public:
int fun()
{
cout<<"Derived::fun() called";
}
};
int main()
{
Derived d;
d.fun(5); // Compiler Error
return 0;
}
Error :
In function 'int main()':
Line 30: error: no matching function for call to 'Derived::fun(int)'
compilation terminated due to -Wfatal-errors.
but just wanna know the reason behind it? why is it not calling fun(int i) method of Base Class since Derived class is derived from Base
The fundamental reason is to make code more robust.
struct Base {
};
struct Derived : Base {
void f(long);
void g() { f(3); } // calls Derived::f
}
Now suppose Base is defined in a library, and you get an update to that library and the update changes the definition of Base:
struct Base {
void f(int);
};
Now suppose that searches for overloaded functions didn't stop when a name was found. In that case, Derived::g would call Base::f instead of Derived::f, and your derived class would quietly do something completely different from what it did before, and different from what it was designed and documented to do.
You've already discovered that derived-class overloads will shadow (prevent the visibility of) base-class methods by the same name but different parameters. Let's just claim this was done for some historical or perceived safety reason, and look at a fix:
class Derived: public Base
{
public:
using Base::fun; // expose the base-class method
int fun()
{
cout<<"Derived::fun() called";
}
};
This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 5 years ago.
I'm normally a C# guy, and it handles this fine, largely under its handling of "best match" resolving the method to call. I'm trying to do something similar in C++ now, but am getting a compile error. Long story short, it's a combination of method overloading and overriding.
class Bar : public Foo { } //Contents don't really matter here
class Base
{
public:
virtual void Do(Foo* foo) { }
virtual void Do(Bar* bar) { }
};
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
}
Foo* foo = new Foo();
Derived* d = new Derived();
d->Do(foo); //cannot initialize a parameter of type 'Bar*' with an rvalue of type 'Foo*'
So, it tries to resolve the method against only the methods in Derived rather than recognizing the base class implements a valid match. Again, C# finds the base method. Can C++ not do this, or am I missing something?
A function in a derived class hides any functions of the same name in a base class (even if you're overriding a virtual function). To unhide the other overloads, use a "using" statement:
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
using Base::Do;
};
This question already has answers here:
name hiding and fragile base problem
(3 answers)
Closed 7 years ago.
Quite a simple question actually but one which is bugging me a lot and I'm unable to find a sound answer for it.
How is the function call d.show(); NOT ambiguous for the following code AND why does b->show(); after b = &d; leads to the calling of Base_::show()?
#include <iostream>
using std::cout;
class Base_
{
public:
void show()
{
cout<<"\nBase Show";
}
};
class Derived_ : public Base_
{
public:
void show()
{
cout<<"Derived Show"<<endl;
}
};
int main()
{
Base_ b;
Derived_ d;
b->show();
d.show();
}
This is called "Name Hiding" in C++. Essentially, if a class defines a function, it hides all functions with the same name from parent classes. Also worth noting, if you had a Base_* to a derived object, and called that function, it would call the base classes version as it is not virtual and will not attempt to find overrided implementations in derived classes.
recently i came to know this - if a derived class redefines base class member method(s) then all the base class methods with same name become hidden in derived class.
#include<iostream>
using namespace std;
class Base
{
public:
int fun()
{
cout<<"Base::fun() called";
}
int fun(int i)
{
cout<<"Base::fun(int i) called";
}
};
class Derived: public Base
{
public:
int fun()
{
cout<<"Derived::fun() called";
}
};
int main()
{
Derived d;
d.fun(5); // Compiler Error
return 0;
}
Error :
In function 'int main()':
Line 30: error: no matching function for call to 'Derived::fun(int)'
compilation terminated due to -Wfatal-errors.
but just wanna know the reason behind it? why is it not calling fun(int i) method of Base Class since Derived class is derived from Base
The fundamental reason is to make code more robust.
struct Base {
};
struct Derived : Base {
void f(long);
void g() { f(3); } // calls Derived::f
}
Now suppose Base is defined in a library, and you get an update to that library and the update changes the definition of Base:
struct Base {
void f(int);
};
Now suppose that searches for overloaded functions didn't stop when a name was found. In that case, Derived::g would call Base::f instead of Derived::f, and your derived class would quietly do something completely different from what it did before, and different from what it was designed and documented to do.
You've already discovered that derived-class overloads will shadow (prevent the visibility of) base-class methods by the same name but different parameters. Let's just claim this was done for some historical or perceived safety reason, and look at a fix:
class Derived: public Base
{
public:
using Base::fun; // expose the base-class method
int fun()
{
cout<<"Derived::fun() called";
}
};
This question already has answers here:
Function with same name but different signature in derived class not found
(2 answers)
Closed 5 years ago.
I need to understand why C++ don't allow to access Grandparent overloaded functions in Child if any of the overloaded function is declared in Parent. Consider the following example:
class grandparent{
public:
void foo();
void foo(int);
void test();
};
class parent : public grandparent{
public:
void foo();
};
class child : public parent{
public:
child(){
//foo(1); //not accessible
test(); //accessible
}
};
Here, two functions foo() and foo(int) are overloaded functions in Grandparent. But foo(int) is not accessible since foo() is declared in Parent (doesn't matter if it declared is public or private or protected). However, test() is accessible, which is right as per OOP.
I need to know the reason of this behavior.
The reason is method hiding.
When you declare a method with the same name in a derived class, base class methods with that name are hidden. The full signature doesn't matter (i.e. cv-qualifiers or argument list).
If you explicitly want to allow the call, you can use
using grandparent::foo;
inside parent.
Just imagine that a library has this class:
struct Base {
};
In your code you use that class as a base class:
struct Derived : Base {
void f(int);
};
and now you write:
Derived d;
d.f('a');
And now you get the shiny new version 2.0 of that library, and the base class has changed a bit:
struct Base {
void f(char);
}
If overloading applied here, your code would break.