Solved

Overloading inherited functions?

Posted on 2003-10-31
12
317 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 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
c++ getting the first 10 characters of a char* string 11 94
Unable to start eclipse ? 17 141
Autosar OS Multicore Share Resources confusion ? 2 39
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

777 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