Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Overloading inherited functions?

Posted on 2003-10-31
12
Medium Priority
?
327 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 1360 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 400 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 240 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

610 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