C 复制分配运算符,用于参考对象变量
c++ copy assignment operator for reference object variable
我给出以下示例以说明我的问题:
class Abc
{
public:
int a;
int b;
int c;
};
class Def
{
public:
const Abc& abc_;
Def(const Abc& abc):abc_(abc) { }
Def& operator = (const Def& obj)
{
// this->abc_(obj.abc_);
// this->abc_ = obj.abc_;
}
};
在这里,我不知道如何定义复制分配运算符。你有什么想法?谢谢。
不能分配给。您需要可以的东西。指针会起作用,但它们非常滥用。
std::reference_wrapper
?
#include <functional>
class Abc
{
public:
int a;
int b;
int c;
};
class Def
{
public:
std::reference_wrapper<const Abc> abc_;
Def(const Abc& abc):abc_(abc) { }
// rule of zero now supplies copy/moves for us
// use the reference
Abc const& get_abc() const {
return abc_.get();
}
};
无法分配参考。因此,只能通过新的和复制结构来定义它:
Def& operator = (const Def& obj)
{
this->~Def(); // destroy
new (this) Def(obj); // copy construct in place
}
,但这确实是不确定的。只需使用指针。
相关文章:
- 通用参考 l 值不复制对象
- 类的方法和对象。参考?智能指针?简单的初始化?
- JNI返回Java对象,可以返回本地参考,还是必须是全局
- neak_ptr vs unique_ptr参考 - 传递接口inmand tos to其他对象
- 如何使对象通过RVALUE参考通过而没有复制
- 超载函数既不按值,也不是通过参考来对象,而是将撤销的指针换成对象
- 通过参考返回C 中的对象
- C 通过参考返回本地对象
- 对矢量中具有常数参考属性的对象进行排序
- 无法将类对象转换为函数默认参数中的参考
- 将参考作为C 中的对象传递
- 常量参考延长对象的寿命,然后是const_cast,这是一个好主意吗?
- 面向对象编程 - 位域私有结构的公共参考
- 在C 中进行可选参考,保留对象寿命
- C 将对象删除为参考
- 升压信号参考对象超出范围?
- C 复制分配运算符,用于参考对象变量
- STD :: BITSET ::参考对象在STD :: BITSET ::操作员[]中创建的寿命
- 参考对象的向量
- 未定义的参考(对象变量和基础变量),C++AVR