gpt4 book ai didi

c++ - 为什么 C++ 允许返回对私有(private)成员的引用

转载 作者:太空宇宙 更新时间:2023-11-04 15:43:47 25 4
gpt4 key购买 nike

<分区>

class test {
public:
test(int value = 0): x(value) {}
int& get(){
return x;
}
private:
int x;
};

这将允许客户端代码改变私有(private)成员
这在 C++ 中是合法的,但为什么呢?
在任何情况下您实际上需要打破类封装吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com