Solved

Overloading inherited functions?

Posted on 2003-10-31
12
313 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:Sandra-24
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9656077
Please show your code.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 340 total points
ID: 9656097
This is compiled for me:

class A
{
public:
    void foo(int n)
    {
    }

    void foo(char* n)
    {
    }

    void foo(bool n)
    {
    }
};

class B : public A
{
    void foo(int n, char c)
    {
        A::foo(n);
    }

    void foo(char* n, char c)
    {
    }

    void foo(bool n, char c)
    {
    }

};


But if I change to:

    void foo(int n, char c)
    {
        foo(n);
    }

it is not compiled.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 9656134
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
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9656145
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
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9656159
Actually, I don't know why. Just found a way to solve the problem. :-)
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 9656350
> 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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 100 total points
ID: 9656372
Basically this is the default inheritance behavioour of C++

Also, Overloading always takes place in the same context.

When u inherit, the context changes, so it is no longer overloading.

HEnce whenver u define a function with the same name in the derived class, the base class function get hidden. [The parameters do not matter nor does the return type]

That does not mean that the function gets overridden.

Overridding only happens when u declare the base class function as virtual

Hope this helps
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9656417
I tried also with virtual, the same behaviour as in my previous post.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9656451
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

0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9656466
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.

0
 
LVL 9

Assisted Solution

by:_ys_
_ys_ earned 60 total points
ID: 9656493
Technically it's known as 'hiding'.

When a dervied class implements a function with the 'same name', the base implemtation goes out of scope. It's as if it doesn't exist. This is obvious given Base::foo(int) and Derived::foo(int) - you'd want Derived::foo(int) to be called obviously.

What's more important, but less known, is that the same rule applies even when the parameters differ.


On a related note, avoid overloading default parameters.

virtual Base::foo (int i = 10) { cout << "Base: " << i; }
Derived::foo (int i = 20)  { cout << "Derived: " << i; }

Derived derived;
Base *pBase = dynamic_cast<Base*>(&derived);

pBase->foo( ); output ??
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9664751
Thanks, now I understand. This 'hiding' feature is very usefull. It may let me solve some programming problems without resorting to virtual.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now