C++ - 为什么可以在复制构造函数中直接访问传递的对象的私有变量?
C++ - Why can a passed object's private variables be accessed directly in the Copy Constructor?
class IntVec
{
public:
IntVec();
IntVec(int siz);
IntVec(const IntVec& temp);
~IntVec();
private:
int* arr;
int size;
};
IntVec::IntVec()
{
size = 2;
arr = new int[size];
}
IntVec::IntVec(int siz)
{
size = siz;
arr = new int[size];
}
IntVec::IntVec(const IntVec& temp)
{
size = temp.size; // Why does this not cause an error? size is a private variable of object temp.
arr = new int[size];
for (int i = 0; i < size; i++)
{
arr[i] = temp.arr[i]; // Why doesn't this cause an error? arr is a private variable of object temp.
}
}
IntVec::~IntVec()
{
delete[] arr;
}
int main()
{
IntVec test1(2);
cout << test1.size; // Causes error (expected), since size is private.
}
我不清楚为什么我可以在复制构造函数中访问 temp 的 size 和 arr 变量,因为它们是私有变量。为什么我在 main() 中出错是有道理的,但我不确定为什么我在复制构造函数中没有出错。
这是因为 public/protected/private 指的是 class 而不是单个对象。因此,class 中的所有对象以及静态方法都可以访问彼此的内部结构。
private成员有class作用域意味着可以在class内部直接使用,不能直接在class外部使用private成员,因为拷贝构造函数在[=]内部12=] 你可以访问私有成员
size = temp.size; // Why does this not cause an error? size is a private variable of object temp.
你误解了访问规则。
访问规则不适用于每个对象。
它们适用于类型和函数。
temp
的类型是IntVec
。 IntVec
类型对象的任何成员都可以在 IntVec
.
的任何成员函数中访问
Non-member 函数和其他 类 的成员函数将无法访问 IntVec
.[=18= 类型对象的 private
成员]
那是因为access specifiers有效per-class,而不是per-object。因此,class 方法可以访问 class.
的 any 实例的私有成员
All members of a class (bodies of member functions, initializers of member objects, and the entire nested class definitions) have access to all the names to which a class can access. A local class within a member function has access to all the names the member function itself can access.
class IntVec
{
public:
IntVec();
IntVec(int siz);
IntVec(const IntVec& temp);
~IntVec();
private:
int* arr;
int size;
};
IntVec::IntVec()
{
size = 2;
arr = new int[size];
}
IntVec::IntVec(int siz)
{
size = siz;
arr = new int[size];
}
IntVec::IntVec(const IntVec& temp)
{
size = temp.size; // Why does this not cause an error? size is a private variable of object temp.
arr = new int[size];
for (int i = 0; i < size; i++)
{
arr[i] = temp.arr[i]; // Why doesn't this cause an error? arr is a private variable of object temp.
}
}
IntVec::~IntVec()
{
delete[] arr;
}
int main()
{
IntVec test1(2);
cout << test1.size; // Causes error (expected), since size is private.
}
我不清楚为什么我可以在复制构造函数中访问 temp 的 size 和 arr 变量,因为它们是私有变量。为什么我在 main() 中出错是有道理的,但我不确定为什么我在复制构造函数中没有出错。
这是因为 public/protected/private 指的是 class 而不是单个对象。因此,class 中的所有对象以及静态方法都可以访问彼此的内部结构。
private成员有class作用域意味着可以在class内部直接使用,不能直接在class外部使用private成员,因为拷贝构造函数在[=]内部12=] 你可以访问私有成员
size = temp.size; // Why does this not cause an error? size is a private variable of object temp.
你误解了访问规则。
访问规则不适用于每个对象。
它们适用于类型和函数。
temp
的类型是IntVec
。 IntVec
类型对象的任何成员都可以在 IntVec
.
Non-member 函数和其他 类 的成员函数将无法访问 IntVec
.[=18= 类型对象的 private
成员]
那是因为access specifiers有效per-class,而不是per-object。因此,class 方法可以访问 class.
的 any 实例的私有成员All members of a class (bodies of member functions, initializers of member objects, and the entire nested class definitions) have access to all the names to which a class can access. A local class within a member function has access to all the names the member function itself can access.