problem about member function overloading

In my project, I want implement a child class which inherits from parent class and overload a member function in the parent class which has been overloaded in the parent class. The condition I meet can be summarized in the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

using namespace std;

class parent {
public:
	parent(){}
	~parent(){}
	virtual void func(int n) {cout<<"parent::func(int)"<<endl;}
	void func(string c) {cout<<"parent::func(string)"<<endl;}
};

class child : public parent {
public:
	child(){}
	~child(){}
	void func(int n) {cout<<"child::func(int)"<<endl;}
};

int main()
{
	child ch;
	string str;
	ch.func(str);
}

The code can't be compiled correctly. The compiler complained there is no function "child::func(string)". I wonder whether I have to copy the code of parent::func(string) to the child class in this condition. Thanks for replies.
The int version is hiding the string version.

You can "unhide" it with the using directive:

1
2
3
4
5
6
7
8
9
class child : public parent {
public:
	child(){}
	~child(){}
	void func(int n) {cout<<"child::func(int)"<<endl;}

	// this line unhides it:
	using parent::func;
};


I don't understand the rationale of why C++ does it that way. Seems rather silly to me.
Topic archived. No new replies allowed.