为什么编译成功?
Why is this compiling successfully?
这段代码编译通过的原因是什么:
#include <iostream>
using namespace std;
class being {
public:
void running(char c) {
cout << "No one know ";
}
};
class human :public being {
public:
using being::running;
void running(char y) {
cout << "I am a human";
}
};
int main() {
human o;
o.running('A');
return 0;
}
the output : "I am a human"
我的意思是(我预计会有错误(人类的重新定义函数 class ))
像这样 :
此代码编译:
#include <iostream>
using namespace std;
class being {
public:
int v;
};
struct human :public being {
public:
double v;
};
int main() {
human o;
o.v = 55.2;
return 0;
}
但是当我添加(使用 being::v )
#include <iostream>
using namespace std;
class being {
public:
int v;
};
struct human :public being {
public:
using being::v;
double v;
};
int main() {
human o;
o.v = 55.2;
return 0;
}
出现错误:error C2086: 'int being::v': redefinition
为什么这个错误没有出现在第一个代码中?
这是 using declaration 的预期行为。
If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
所以human::running(char)
隐藏being::running(char)
而不是冲突
编辑
第二个代码片段格式错误。对于数据成员,根据标准,[namespace.udecl]/10:
If a declaration named by a using-declaration that inhabits the target scope of another declaration potentially conflicts with it ([basic.scope.scope]), and either is reachable from the other, the program is ill-formed. If two declarations named by using-declarations that inhabit the same scope potentially conflict, either is reachable from the other, and they do not both declare functions or function templates, the program is ill-formed.
[Example 6:
...
namespace B {
int i;
...
}
void func() {
int i;
using B::i; // error: conflicts
...
...
所以你不能 using
being::v
与 human::v
冲突。但是对于成员函数,[namespace.udecl]/11:
The set of declarations named by a using-declarator that inhabits a class C does not include member functions and member function templates of a base class that correspond to (and thus would conflict with) a declaration of a function or function template in C.
[Example 7:
struct B {
virtual void f(int);
virtual void f(char);
void g(int);
void h(int);
};
struct D : B {
using B::f;
void f(int); // OK: D::f(int) overrides B::f(int);
using B::g;
void g(char); // OK
using B::h;
void h(int); // OK: D::h(int) hides B::h(int)
};
...
这段代码编译通过的原因是什么:
#include <iostream>
using namespace std;
class being {
public:
void running(char c) {
cout << "No one know ";
}
};
class human :public being {
public:
using being::running;
void running(char y) {
cout << "I am a human";
}
};
int main() {
human o;
o.running('A');
return 0;
}
the output : "I am a human"
我的意思是(我预计会有错误(人类的重新定义函数 class )) 像这样 : 此代码编译:
#include <iostream>
using namespace std;
class being {
public:
int v;
};
struct human :public being {
public:
double v;
};
int main() {
human o;
o.v = 55.2;
return 0;
}
但是当我添加(使用 being::v )
#include <iostream>
using namespace std;
class being {
public:
int v;
};
struct human :public being {
public:
using being::v;
double v;
};
int main() {
human o;
o.v = 55.2;
return 0;
}
出现错误:error C2086: 'int being::v': redefinition
为什么这个错误没有出现在第一个代码中?
这是 using declaration 的预期行为。
If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
所以human::running(char)
隐藏being::running(char)
而不是冲突
编辑
第二个代码片段格式错误。对于数据成员,根据标准,[namespace.udecl]/10:
If a declaration named by a using-declaration that inhabits the target scope of another declaration potentially conflicts with it ([basic.scope.scope]), and either is reachable from the other, the program is ill-formed. If two declarations named by using-declarations that inhabit the same scope potentially conflict, either is reachable from the other, and they do not both declare functions or function templates, the program is ill-formed.
[Example 6:
... namespace B { int i; ... } void func() { int i; using B::i; // error: conflicts ... ...
所以你不能 using
being::v
与 human::v
冲突。但是对于成员函数,[namespace.udecl]/11:
The set of declarations named by a using-declarator that inhabits a class C does not include member functions and member function templates of a base class that correspond to (and thus would conflict with) a declaration of a function or function template in C.
[Example 7:
struct B { virtual void f(int); virtual void f(char); void g(int); void h(int); }; struct D : B { using B::f; void f(int); // OK: D::f(int) overrides B::f(int); using B::g; void g(char); // OK using B::h; void h(int); // OK: D::h(int) hides B::h(int) }; ...