c++ - 复制和移动构造函数是自动朋友吗?
问题描述
当我们定义复制或移动构造函数时,我们可以访问另一个类的私有变量。C ++会friend
自动将它们相互连接吗?
例如:
my_str::my_str(my_str&& m)
{
size_ = m.size_; //accessing private variable another my_str class
buff_ = m.buff_; //accessing private variable another my_str class
m.buff_ = nullptr;
m.size_ = 0;
}
解决方案
它不被认为是朋友,但是是的,类的任何成员函数都my_str
可以访问 type 的所有实例的私有成员my_str
,而不仅仅是this
实例:
class my_str {
void foo(my_str& other) {
// can access private members of both this-> and other.
}
static void bar(my_str& other) {
// can access private members of other.
}
};
其背后的总体思想是允许 2 个或多个相同类型的对象进行交互,而不必暴露它们的私有成员。
推荐阅读
- retrofit - Retrofit & RxJava : 执行多个独立请求并检测它们何时全部完成
- php - 使用 str_replace 转义简码
- javascript - 从数组中的多个对象中过滤属性
- php - Drupal 7搜索API挂钩注册回调类未触发
- node.js - 输出对我的 webhook url 发出的 POST 请求(JSON 数据)?
- python - 为什么这个循环有“2 for i in range”?它有什么作用?
- regex - 如何从 `[[:punct:]]` 中获取 Ruby Regexp 标点符号列表?
- r - 结构主题模型(stm包)使用绘图函数绘制百分比值
- node.js - 如何在 Node.js 中的短信验证中制作字符串链接
- javascript - 如何在javascript中获取cookie的特定值