o******r 发帖数: 259 | 1 以下程序似乎是对的,在Visual Studio 2005里面却报错:
error C2660: 'B::Add' : function does not take 1 arguments
#include
using namespace std;
class A
{
public:
int Add(int X) {return Add(X, 1);};
private:
virtual int Add(int X, int Y) {return X+Y;};
};
class B: public A
{
private:
int Add(int X, int Y) {return X-Y;};
};
int main()
{
B Obj;
cout << Obj.Add(5);
return 0;
}
难道B不能用base clas A的那个public function吗?
用不同函数名就没问题了
#include
using namespac | P*****f 发帖数: 2272 | 2 name hiding
以下程序似乎是对的,在Visual Studio 2005里面却报错:
error C2660: 'B::Add' : function does not take 1 arguments
#include
using namespace std;
class A
{
public:
int Add(int X) {return Add(X, 1);};
private:
virtual int Add(int X, int Y) {return X+Y;};
};
class B: public A
{
private:
int Add(int X, int Y) {return X-Y;};
};
int main()
{
B Obj;
cout << Obj.Add(5);
return 0;
}
难道B不能用base clas A的那个public function吗?
用不同函数名就没问题了
#include
u
【在 o******r 的大作中提到】 : 以下程序似乎是对的,在Visual Studio 2005里面却报错: : error C2660: 'B::Add' : function does not take 1 arguments : #include : using namespace std; : class A : { : public: : int Add(int X) {return Add(X, 1);}; : private: : virtual int Add(int X, int Y) {return X+Y;};
| o******r 发帖数: 259 | 3 what do you mean?
【在 P*****f 的大作中提到】 : name hiding : : 以下程序似乎是对的,在Visual Studio 2005里面却报错: : error C2660: 'B::Add' : function does not take 1 arguments : #include : using namespace std; : class A : { : public: : int Add(int X) {return Add(X, 1);};
| P*****f 发帖数: 2272 | 4 edited
3.3.7 Name hiding
...
The declaration of a member in a derived class (clause 10) hides the
declaration of a member of a base class of the same name; see 10.2.
4 During the lookup of a name qualified by a namespace name, declarations
that would otherwise be made
visible by a using-directive can be hidden by declarations with the same
name in the namespace containing
the using-directive; see (3.4.3.2).
what do you mean?
【在 o******r 的大作中提到】 : what do you mean?
| o******r 发帖数: 259 | 5 thanks for the info
after goole, the workaround is,
class A
{
public:
int Add(int X) {return Add(X, 1);};
protected:
virtual int Add(int X, int Y) {return X+Y;};
};
class B: public A
{
public:
using A::Add;
private:
int Add(int X, int Y) {return X-Y;};
};
【在 P*****f 的大作中提到】 : edited : 3.3.7 Name hiding : ... : The declaration of a member in a derived class (clause 10) hides the : declaration of a member of a base class of the same name; see 10.2. : 4 During the lookup of a name qualified by a namespace name, declarations : that would otherwise be made : visible by a using-directive can be hidden by declarations with the same : name in the namespace containing : the using-directive; see (3.4.3.2).
| N*********y 发帖数: 105 | 6 Read Effective C++, the item for name-hiding, can't remember the item #,
should be 3X
【在 o******r 的大作中提到】 : 以下程序似乎是对的,在Visual Studio 2005里面却报错: : error C2660: 'B::Add' : function does not take 1 arguments : #include : using namespace std; : class A : { : public: : int Add(int X) {return Add(X, 1);}; : private: : virtual int Add(int X, int Y) {return X+Y;};
|
|