Solved

Overloading inherited functions?

Posted on 2003-10-31
12
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 31

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 31

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 104
I could not build boost code, 10 107
Unable to start eclipse ? 17 163
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 76
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

696 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