简体   繁体   English

C ++ operator =用于具有类成员函数的数组

[英]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_arrayunsigned 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.

相关问题 在类成员operator()上的c ++ typeid重载 - c++ typeid on class member operator() overloads 运算符+/-重载作为C ++中的非成员函数 - Operator +/- overloading as a non member function in C++ 在C ++中,运算符[]()必须是成员函数吗? - In C++, must operator[] () be a member function? C ++类成员函数不修改成员 - C++ class member function not modifying member C ++运算符重载输入运算符&gt;&gt;,并将类外部的对象作为成员 - C++ operator overloading input operator >> with object outside class as member C++类成员函数转c函数 - C++ class member function to c function C ++使用成员选择运算符(。或–&gt;)访问静态成员函数 - c++ accessing static member function with member-selection operator (. or –>) c ++ STL map.find()或map.operator []不能在具有const限定符的类成员函数中使用 - c++ STL map.find() or map.operator[] cannot be used in class member function with const qualifier 重载运算符和C ++中同一类的成员函数之间的优先顺序 - Precedence order between overloaded operator and member function of the same class in c++ 如何将 class 对象传递给 ZF6F87C9FDCF8B3C3F07ZF93F1EE8712C 中的另一个 class 成员 arrays - how to pass class objects to another class member arrays in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM