Why can't I overload functions from a base class?

Aug 6, 2009 at 5:17am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

enum eTestB{ B };

class UseA{
public: void Testing( float Type ){}
};

class UseB : public UseA{
public: void Testing( eTestB Type ){}
};

void main(){
	UseB Var;

	Var.Testing( 0.5f );
}


Will result in a error "cannot convert param 1 from 'float' to 'eTestB'.

Why is this?
Last edited on Aug 6, 2009 at 5:18am
Aug 6, 2009 at 6:14am
That is because what you have is not overloading, it is actually function hiding.

A function in a derived class that has the same name as a function in a base class hides ALL those functions with the same name in the base class.

Note:
It is not function overriding, because function overriding is for virtual functions
Aug 6, 2009 at 6:41am
Ok thanks, I googled it for a solution and came to this page:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9

What is the standard way of taking care of this issue? By 'using' or by redeclaring the function in the derived class? Or some other way?
Aug 6, 2009 at 7:12am
You have (at least) three possibilities - it all depends on what you want to achieve:

1. Rename the derived class function(s) to avoid the name clash

2. Use the using directive.

3. Redeclaring the function in the derived class, and in that function call the base class function.
This is useful, if you want to block direct access to the base class function, maybe you want to do something like make a log or maybe add some debugging code, then call the base class version.
Aug 6, 2009 at 7:13am
By the way - that FAQ you found is really good - I use it quite often.
Topic archived. No new replies allowed.