[英]C++ operator = for arrays with class member function
I have a wrapper data structure that I'd like to write a method for the copy assignment operator. 我有一个包装器数据结构,我想为复制赋值运算符编写一个方法。 I'd really like to do this (because my
byte_array
can do some really cool stuff): 我真的很喜欢这样做(因为我的
byte_array
可以做一些非常酷的东西):
byte_array bytes = {0x00, 0xAB, 0xEE, 0xFF};
and the function I'm trying to write looks like this so far: 到目前为止,我正在尝试编写的函数看起来像这样:
template <int N>
byte_array& byte_array::operator = (const unsigned char (&a)[N]) {
if(N <= capacity) { //capacity is a class field
for(int i = 0; i < N; i++) array[i] = a[i]; //array is a class field
usage = N; //usage is a class field
}
return *this;
}
However, this code doesn't compile, as it does not like that I gave it a list. 但是,这段代码没有编译,因为它不喜欢我给它一个列表。 It doesn't like a lot of things about this.
它不喜欢这方面的很多事情。
Is there something wrong with the function I wrote, such as the assignment operator only working on objects of the same type (like a conflict of byte_array
and unsigned char[]
)? 我写的函数有什么问题,比如赋值运算符只处理相同类型的对象(比如
byte_array
和unsigned char[]
的冲突)? Is it an issue of my 'list' that I'm trying to assign it to not matching up with the unsigned char[]
type? 这是我的'列表'的一个问题,我试图将其分配给不匹配的
unsigned char[]
类型?
I was basing my function off of an answer to this past question about arrays in C++ . 我的功能基于对过去关于C ++中数组的问题的回答。
The issue with 这个问题
byte_array bytes = {0x00, 0xAB, 0xEE, 0xFF};
is that you you do not actually have an array on the right hand side. 是你实际上没有右侧的阵列。 You have an initializer list , which doesn't have a type.
您有一个初始化列表 ,它没有类型。
What you can do though is take a std::initializer_list<unsigned char>
and that can be constructed from the initializer list . 你可以做的是采用
std::initializer_list<unsigned char>
,并且可以从初始化列表构造。
In C++98 you can create a temporary array and then use that array from the assignemnt like 在C ++ 98中,您可以创建一个临时数组,然后使用来自assignemnt的数组
byte_array bytes;
//some code
{
unsigned char temp[] = {0x00, 0xAB, 0xEE, 0xFF};
bytes = temp;
}
// more code
Also note that Type name = initializer;
另请注意
Type name = initializer;
is never assignment. 绝不是任务。 It is called copy initialization / copy list initialization (if
initializer
is a initializer list ), and calls the copy/move constructor of the object. 它被称为复制初始化 / 复制列表初始化 (如果
initializer
是初始化器列表 ),并调用该对象的复制/移动构造函数。 The assignment operator is only ever called then you have an already constructed object. 只调用赋值运算符,然后你就已经构造了一个对象。
the issue is that you are trying to assign an initializer_list {a, b, c} and not an array. 问题是您正在尝试分配initializer_list {a,b,c}而不是数组。 You might want to take an initializer list as the parameter instead:
您可能希望将初始化列表作为参数:
#include <iostream>
struct byte_array
{
byte_array& operator = (const std::initializer_list<unsigned char>& tab) {
for (unsigned char item: tab)
std::cout << item << "\n";
return *this;
}
};
int main()
{
byte_array bytes;
bytes = {'a', 'b', 'c', 'd'};
(void)bytes;
}
run it here: http://cpp.sh/2vv6m 在这里运行: http : //cpp.sh/2vv6m
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.