这两个陈述有什么区别?
ob.A::ar[0] = 200;
ob.ar[0] = 200;
Run Code Online (Sandbox Code Playgroud)
哪个ob
是类的对象A
class A
{
public:
int *ar;
A()
{
ar = new int[100];
}
};
Run Code Online (Sandbox Code Playgroud)
没有区别.ar
在这种情况下,显式名称空间限定是多余的.
在(多个非虚拟)继承重新定义名称的情况下,它可能不是多余的ar
.样本(人为):
#include <string>
class A
{
public:
int *ar;
A() { ar = new int[100]; }
// unrelated, but prevent leaks: (Rule Of Three)
~A() { delete[] ar; }
private:
A(A const&);
A& operator=(A const&);
};
class B : public A
{
public:
std::string ar[12];
};
int main()
{
B ob;
ob.A::ar[0] = 200;
ob.ar[0] = "hello world";
}
Run Code Online (Sandbox Code Playgroud)
请参阅http://liveworkspace.org/code/d25889333ec378e1382cb5af5ad7c203
在这种情况下,没有区别.
这种表示法:
obj.Class::member
只是为了解决继承的模糊性:
class A {
public:
int a;
}
class B {
public:
int a;
}
class C : public A, B {
void func() {
// this objects holds 2 instance variables of name "a" inherited from A and B
this->A::a = 1;
this->B::a = 2;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
205 次 |
最近记录: |