如何访问通过引用传递给operator= function的对象的私有数据?
How do i access the private data of an object which is passed by reference to operator= function?
我想知道我如何能够访问通过引用或值传递的对象的私有数据?这段代码可以工作。为什么?我需要一些解释。
class test_t {
int data;
public:
test_t(int val = 1): data(val){}
test_t& operator=(const test_t &);
};
test_t& test_t::operator=(const test_t & o){
this->data = o.data;
return *this;
}
private
表示test_t
类的所有实例都可以看到彼此的私有数据。
如果c++更严格,并且限制private
对同一实例中的方法的访问,那么它将有效地说*this
的类型比o
引用的类型"更强大"。
*this
的类型与o
(即test_t &
)的类型相同(†),因此o
可以做*this
可以做的任何事情。
(†)相同的类型,除了添加了const
,但这在这里并不重要
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 'operator='已弃用:改用 QDir::setPath()
- 传递给std::function template的template参数究竟代表什么
- 过载'operator new'如何导致无限循环?
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 如何在向量中删除 std::function<void()>?
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- 为什么需要"std::function::operator=(F &&)"来制作临时的"std::function"?
- std::function operator() 和 std::forward 中发生了什么?
- C++ std::function operator=
- 如何访问通过引用传递给operator= function的对象的私有数据?