Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - C++ Overloaded method inheritance question.: (3 Items)
   
C++ Overloaded method inheritance question.  
Hi,

Im am unable to get the following to compile. It errors in main on the first Call function. Does a derived class hide 
methods of the same name with a different sig? I dont want to have to use Base1::Call...

Without going into too much detail I need to be able to build up a class, this is done with template and template 
specialisations, with an overloaded function that is then called in the mannor below.

class Base1 {
public:
	void Call(){}
};

class Base2 : public Base1 {
public:
	void Call(int i){}
};

int main(int argc, char **argv){	
	Base2 func;
	
	func.Call();
	func.Call(5);
	
	return 0;
}

..\src\main.cpp:15: error: no matching function for call to `Base2::Call()'
..\src\main.cpp:9: note: candidates are: void Base2::Call(int)

Is there a way to get this to work?

Kind Regards,

Paul

Re: C++ Overloaded method inheritance question.  
Base1:Call() needs a void in the parameter list.
You will need either a Base2::Call(void) or set a default value for i to
Base2::Call(int i=0)

Paul Wakefield wrote:
> Hi,
> 
> Im am unable to get the following to compile. It errors in main on the 
> first Call function. Does a derived class hide methods of the same name 
> with a different sig? I dont want to have to use Base1::Call...
> 
> Without going into too much detail I need to be able to build up a 
> class, this is done with template and template specialisations, with an 
> overloaded function that is then called in the mannor below.
> 
> class Base1 {
> public:
>         void Call(){}
> };
> 
> class Base2 : public Base1 {
> public:
>         void Call(int i){}
> };
> 
> int main(int argc, char **argv){       
>         Base2 func;
>        
>         func.Call();
>         func.Call(5);
>        
>         return 0;
> }
> 
> ..\src\main.cpp:15: error: no matching function for call to
`Base2::Call()'
> ..\src\main.cpp:9: note: candidates are: void Base2::Call(int)
> 
> Is there a way to get this to work?
> 
> Kind Regards,
> 
> Paul
> 
> 
> 
> _______________________________________________
> General
> http://community.qnx.com/sf/go/post8299
> 

-- 
cburgess@qnx.com
Re: C++ Overloaded method inheritance question.  
Add this line in Base2's public section:

using Base1::Call;

So, the complete code will be:

#include <iostream>

class Base1 {
public:
	void Call(){ std::cout << "call()\n"; }
};

class Base2 : public Base1 {
public:
	using Base1::Call;
	void Call(int i){ std::cout << "call(int)\n"; }
};

int main(int argc, char **argv){	
	Base2 func;
	
	func.Call();
	func.Call(5);
	
	return 0;
}