C++ 如何为此类C+实现复制操作符+;结构
所以C++ 如何为此类C+实现复制操作符+;结构,c++,oop,class,copy,operators,C++,Oop,Class,Copy,Operators,所以 struct ResultStructure { ResultStructure(const ResultStructure& other) { // copy code in here ? using memcpy ? how??? } ResultStructure& operator=(const ResultStructure& other) { if (this != &o
struct ResultStructure
{
ResultStructure(const ResultStructure& other)
{
// copy code in here ? using memcpy ? how???
}
ResultStructure& operator=(const ResultStructure& other)
{
if (this != &other) {
// copy code in here ?
}
return *this
}
int length;
char* ptr;
};
如何实现“复制构造函数”和“赋值运算符”?(对不起,我是C++ Nube)< /P>
更新:sbi和其他人问-为什么我要手动处理原始内存?我的答案很简单——在我参与的一个学生项目中,我们使用了大量的C库,例如OpenCV OpenAL和FFmpeg,还有更多。目前使用C++,我们试图创建一个基于图形的直接显示,如跨平台库,这将有助于现场视频广播和处理。我们的图形元素目前使用char*和int对进行数据交换。为了将数据强制转换到订阅的元素,我们现在使用原始memcpy。我想进一步做,使我们能够使我们的图形元素的基础上的C++模板。因此,一个图形元素将能够与其他图形元素共享当前图形元素数据,并且它共享的数据将是一个结构,该结构将不包含一个字符*一个int,而是包含任意数量的数据字段和几乎任何元素。这就是为什么我需要理解如何创建一个基本的C++结构,它实现了“复制构造函数”和“赋值操作符”,以便我能够使用新的数据流算法,如
void CastData(T item){
for(size_t i = 0 ; i < FuncVec.size(); i++){
T dataCopy = item;
FuncVec[i](dataCopy);
}
}
void CastData(T项){
对于(size_t i=0;i
而不是当前使用的
void CastData(char * data, int length){
for(size_t i = 0 ; i < FuncVec.size(); i++){
char* dataCopy = new char[length];
memcpy(dataCopy, data, length);
FuncVec[i](dataCopy, length);
delete[] dataCopy;
}
}
void CastData(字符*数据,整数长度){
对于(size_t i=0;i这是std::vector
的工作,还是家庭作业
向量将替换长度
和ptr
后面的分配。向量是C++习惯用法,如果你实现了你所描述的类,你就不能与其他C++开发者建立朋友关系。当然,也有一些特殊情况,但默认情况下会使用标准容器,如vector
向量不仅知道如何复制字符,还知道如何复制自身,并且对实现进行了优化和测试
下面介绍如何使用向量显式实现复制/赋值:
struct ResultStructure {
ResultStructure(const ResultStructure& other) : d_chars(other.d_chars) {
}
ResultStructure& operator=(const ResultStructure& other) {
if (this != &other) {
this->d_chars = other.d_chars;
}
return *this;
}
std::vector<char> d_chars;
};
struct ResultStructure{
结果结构(const ResultStructure&other):d_字符(other.d_字符){
}
结果结构和运算符=(常量结果结构和其他){
如果(此!=&其他){
此->d_字符=其他d_字符;
}
归还*这个;
}
std::向量d_字符;
};
这是std::vector
的工作,还是家庭作业
向量将替换长度
和ptr
后面的分配。向量是C++习惯用法,如果你实现了你所描述的类,你就不能与其他C++开发者建立朋友关系。当然,也有一些特殊情况,但默认情况下会使用标准容器,如vector
向量不仅知道如何复制字符,还知道如何复制自身,并且对实现进行了优化和测试
下面介绍如何使用向量显式实现复制/赋值:
struct ResultStructure {
ResultStructure(const ResultStructure& other) : d_chars(other.d_chars) {
}
ResultStructure& operator=(const ResultStructure& other) {
if (this != &other) {
this->d_chars = other.d_chars;
}
return *this;
}
std::vector<char> d_chars;
};
struct ResultStructure{
结果结构(const ResultStructure&other):d_字符(other.d_字符){
}
结果结构和运算符=(常量结果结构和其他){
如果(此!=&其他){
此->d_字符=其他d_字符;
}
归还*这个;
}
std::向量d_字符;
};
如果您使用std::string
,而不是char*
,您甚至不需要编写操作符=
或复制构造函数。编译器生成的代码将很好地完成您的工作
但作为一般解决方案(对于某些其他场景),请使用复制和交换习惯用法:
作者:Herb Sutter非常详细地描述了这些。我建议你阅读这本书中的内容。目前,您可以在线阅读这篇文章:
如果您使用std::string
,而不是char*
,您甚至不需要编写操作符=
或复制构造函数。编译器生成的代码将很好地完成您的工作
但作为一般解决方案(对于某些其他场景),请使用复制和交换习惯用法:
作者:Herb Sutter非常详细地描述了这些。我建议你阅读这本书中的内容。目前,您可以在线阅读这篇文章:
如何分配ptr
点的内存
如果使用“新建”,请使用new
进行分配,设置length
,然后复制
other.length = length;
other.ptr = new char[length];
memcpy( other.ptr, ptr, length );
如果要使用malloc
分配内存,请用调用malloc
代替调用new
。如果您正在使用其他内存方案,请找出它。:) 如何分配ptr
点数的内存
如果使用“新建”,请使用new
进行分配,设置length
,然后复制
other.length = length;
other.ptr = new char[length];
memcpy( other.ptr, ptr, length );
如果要使用malloc
分配内存,请用调用malloc
代替调用new
。如果您正在使用其他内存方案,请找出它。:) 简单的解决方案是使用std::string
而不是char*
成员
然后编译器生成的复制构造函数和复制赋值运算符就可以工作了
作为一项规则,尤其是作为新手,不要有原始指针成员
干杯,简单的解决方案是使用std::string
而不是char*
成员
然后编译器生成的复制构造函数和复制赋值运算符就可以工作了
作为一项规则,尤其是作为新手,不要有原始指针成员
干杯,嗯。我认为这应该可以完成以下工作:
struct ResultStructure
{
ResultStructure(const ResultStructure& other);
ResultStructure& operator=(const ResultStructure& other);
int length;
char* ptr;
};
ResultStructure::ResultStructure(const ResultStructure& other):length(other.length)
{
ptr = (char*)malloc(length);
memcpy(ptr, other.ptr, length);
}
ResultStructure& ResultStructure::operator=(const ResultStructure& other)
{
length = other.length;
ptr = (char*)malloc(length);
memcpy(ptr, other.ptr, length);
return *this;
}
请记住在析构函数中释放ptr。
什么是商店
struct ResultStructure
{
ResultStructure(const ResultStructure& other)
: ptr(new char[rhs.length]), length(rhs.length)
{
std::copy(rhs.ptr, rhs.ptr+rhs.length, ptr);
}
~ResultStructure() // your class needs this
{
delete[] ptr;
}
ResultStructure& operator=(ResultStructure rhs) // note: passed by copy
{
this->swap(rhs);
return *this
}
void swap(const ResultStruct& rhs)
{
using std::swap;
swap(length, rhs.length);
swap(ptr, rhs.ptr);
}
std::size_t length;
char* ptr;
};
class Result
{
public:
private:
size_t length; // can't really be negative, right ?
char* data;
};
Result::Result(): length(0), data(0) {}
Result::Result(size_t l, char* d): length(0), data(0)
{
if (!d) { return; }
data = new char[l]; // this may throw, so we do it first
length = l;
memcpy(data, d, l);
}
// Swap
void Result::swap(Result& other)
{
using std::swap;
swap(length, other.length);
swap(data, other.data);
}
// Copy Constructor
Result::Result(Result const& other): length(0), data(0)
{
if (!other.length) { return; }
data = new char[other.length];
length = other.length;
mempcy(data, other.data, length);
}
// Assignemt Operator
Result& Result::operator=(Result other)
{
this->swap(other);
return *this;
}
// !IMPORTANT!
// Destructor
Result::~Result()
{
delete[] data; // use the array form of delete
// no need to test for nullity, it's handled
}