Sandra-24
asked on
Overloading inherited functions?
I'm noticing what seems to me to be unusual behaviour but what is more likely just something I misunderstand.
Class B inherits from class A. Class A defines three overloads of void foo(), each with a single parameter.
Class B then defines three more void foo() overloads each with two parameters.
If, however, I try to use the single parameter version of foo() in Class B the compiler tells me there is no such thing.
B::foo no overloaded function takes 1 parameters
But if I comment out the new overloads of foo in class B it works like a charm.
Where am I going wrong? How do I accomplish this (extend the functionality of foo() from class A) in a way that will compile?
Thanks for any light you can shed on this!
-Sandra
Class B inherits from class A. Class A defines three overloads of void foo(), each with a single parameter.
Class B then defines three more void foo() overloads each with two parameters.
If, however, I try to use the single parameter version of foo() in Class B the compiler tells me there is no such thing.
B::foo no overloaded function takes 1 parameters
But if I comment out the new overloads of foo in class B it works like a charm.
Where am I going wrong? How do I accomplish this (extend the functionality of foo() from class A) in a way that will compile?
Thanks for any light you can shed on this!
-Sandra
Please show your code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Sandra-24,
that's default C++ behavior ... if you overload a function in a derived class with different declaration
the base-class's functions ...
you can use 'using' in class B, i.e.
class B : public A
{
using A::foo;
...
};
Hope that helps,
ZOPPO
that's default C++ behavior ... if you overload a function in a derived class with different declaration
the base-class's functions ...
you can use 'using' in class B, i.e.
class B : public A
{
using A::foo;
...
};
Hope that helps,
ZOPPO
ASKER
That's it Alex, I changed everywhere I used foo(int) to A::foo(int) and it worked.
Why is that as a matter of interest?
I didn't realize :: works for non-static functions but I don't understand why this->foo(n) should fail where A::foo(n) succeeds.
Technically foo is part of B because it get's inherited. I'm sure I could access it from outside of B (assuming foo is declared as public).
foo(int) is distinct and shouldn't be mistaken for anything else so why does one have to explicitly declare that we want A's foo?
Thanks,
-Sandra
Why is that as a matter of interest?
I didn't realize :: works for non-static functions but I don't understand why this->foo(n) should fail where A::foo(n) succeeds.
Technically foo is part of B because it get's inherited. I'm sure I could access it from outside of B (assuming foo is declared as public).
foo(int) is distinct and shouldn't be mistaken for anything else so why does one have to explicitly declare that we want A's foo?
Thanks,
-Sandra
Actually, I don't know why. Just found a way to solve the problem. :-)
> I changed everywhere I used foo(int) to A::foo(int) and it worked.
hm ... would have been easier to use the 'using' method ...
IMO this behavior is implemented coz it may prevent from some hard
to find logical bugs within the code ... places where base classes functions
are called accidentally will lead to compile errors unless you expliciteley
say you want to call it.
ZOPPO
hm ... would have been easier to use the 'using' method ...
IMO this behavior is implemented coz it may prevent from some hard
to find logical bugs within the code ... places where base classes functions
are called accidentally will lead to compile errors unless you expliciteley
say you want to call it.
ZOPPO
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I tried also with virtual, the same behaviour as in my previous post.
As I had already posted,
making the function virtual would let u override the same function [name alongwith parameters i.e. same signature] in the derived class
Overloading would not happen in different contexts
making the function virtual would let u override the same function [name alongwith parameters i.e. same signature] in the derived class
Overloading would not happen in different contexts
Actually this behaviour is applicable to all class members i.e. data as well as functions.
If u declare a variable with the same name as one in the base class, then the base class variable gets hidden i.e. U need to use the scope resolution operator to acces it.
Same with functions.
If u declare a variable with the same name as one in the base class, then the base class variable gets hidden i.e. U need to use the scope resolution operator to acces it.
Same with functions.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, now I understand. This 'hiding' feature is very usefull. It may let me solve some programming problems without resorting to virtual.