Redian新闻
>
关于 VC++ vitual, reload 和 derive的一个问题...
avatar
关于 VC++ vitual, reload 和 derive的一个问题...# Programming - 葵花宝典
o*r
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
avatar
P*f
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;};

avatar
o*r
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);};

avatar
P*f
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?
avatar
o*r
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).

avatar
N*y
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;};

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。