Solved

Overloading inherited functions?

Posted on 2003-10-31
12
321 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

690 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