I can't find any difference between function hiding and overloading. As the function hiding is the function that is present in derived class and hides the function of a base class. Having same name of the function in both of them. Overloading: having same name but different signature in both derived and base class.
class A {
void print(int);
};
class B: public A {
void print(float);
};
does it hide function or overload ?
The function B::print
hides the parent function A::print
.
If you want to overload you need to pull in the A::print
function into the scope of B
:
class B : public A {
public:
using A::print; // Pull in (all) A::print symbols into the scope of B
void print(float); // Now overloads A::print
};
A name defined in a scope hides declarations of the same name in any outer scope. Like this:
int name; // global name
void f() {
int name; // name 1, hides global name
for (int i = 0; i < 10; i++) }
int name; // name 2, hides name 1 and global name
name = 3; // assigns 3 to name 2
}
name = 4; // assigns 4 to name 1
}
int other = name; // copies value of global name into other
When two or more functions with the same name are declared in the same scope, the name is overloaded:
// two functions in global scope:
void f();
void f(int);
void g() {
f(); // ok: calls first version of f
f(1); // ok: calls second version of f
}
Definitions in different scopes do not define overload sets, because the declaration in the inner scope hides the same name in any outer scope.
class cl1 {
public:
// hides global f's:
void f();
void g() {
f(); // ok: calls cl1::f()
f(1); // error: no f takes an int; global f(int) is hidden
}
};
Of course, defining more than one function in the same scope still defines overloads, even if there are functions with the same (hidden) name in an outer scope.
class cl2 {
public:
// these hide global f's, and provided overloads:
void f();
void f(int);
void g() {
f(); // calls cl2::f()
f(1); // calls cl2::f(int)
}
};
A class definition provides a new scope, so the rules about name hiding and overloading apply to names defined in the class.
class cl3 : public cl2 {
void g() {
f(); // calls cl2::f()
f(1); // calls cl2::f(int)
}
};
class cl4 : public cl2 {
// hides all f's:
void f();
void g() {
f(); // ok: calls cl4::f();
f(3); // error: no f takes an int; global f(int) and cl2::f(int) are hidden
}
};
class cl5 : public cl2 {
// hides all f's and provides overloads:
void f();
void f(int);
void g() {
f(); // ok: calls cl5::f()
f(3); // ok: calls cl5::f(int)
}
};
You can also use a signature that doesn't match any of the signatures in the base class. After all, the name of those base class functions is hidden.
class cl5 : public cl2 {
public:
// hides all f's:
void f(std::string);
void g() {
f(); // error: no f taking no arguments
f(3); // error: no f taking int
std::string arg("test");
f(arg); // ok: calls cl5::f(std::string)
}
};
And, finally, if you want to write a derived class that adds signatures to the set of overloaded functions defined in a base class, you have to add the name of those overloaded functions to the derived class:
class cl6 : public cl2 {
public:
using cl2::f; // pull cl2::f into current scope
void f(std::string); // add overload to names from cl2
void g() {
f(); // ok: calls cl2::f()
f(3); // ok: calls cl2::f(int)
std::string arg("test");
f(arg); // ok: calls cl6::f(std::string)
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With