简体   繁体   English

重载[]和,c ++ 11

[英]overloading [] and , c++11

So suppose we have this expression 所以假设我们有这个表达

auto x =Object.operator[][1,2,3,4];

I want my C++ code to insert the numbers 1 2 3 4 to a vector that is located inside the object my code is below. 我希望我的C ++代码将数字1 2 3 4插入到位于代码下面的对象内部的向量中。

class Object{
    Object operator[](int temp){
        this->x.push_back(temp);
        return *this;
    }
    vector<int> x;
};
int main()
{
    auto x =Object().operator[][1,2,3,4];

   return 0;
}

This code doesnt compile. 此代码不会编译。 As far as i have understood i have to overload comma and [] but i cant figure out how to do this.I am new to C++. 据我了解,我必须重载逗号和[],但是我不知道该怎么做。我是C ++的新手。

Edit extra question.!!!!!!!!!!!!!!!!!!!!!!!!!! 编辑其他问题。!!!!!!!!

Check code below that is my update. 检查下面的代码是我的更新。

#include <iostream>

using namespace std;
class List{
    public:
    List(){}
    List(int a){
        x.push_back(a);
    }
    ~List(){}
    int operator[](int a){

        return a;
    }
        const char* operator[](const char * a){
        return a;
    }

    vector <int> x;
};
int main()
{
    List x = List{}[1,2,3,4];


   return 0;
}

Thanks to you guys i am close to the answer i am searching.How can i add all the values included in [] to some vector for example and send it to list and add to its vector.There comma operator will be overloaded but i dont know how.As far as i know and i can understand is that i have to put an object before 1 and the overloaded comma will be called but i cannot do that.is there any other special syntax that would call it. 谢谢大家,我很接近我正在寻找的答案。如何将[]中包含的所有值添加到某个向量中,例如,将其发送到列表中并添加到其向量中。逗号运算符将被重载,但我不会据我所知,我能理解的是我必须将一个对象放在1之前,并且将调用重载的逗号,但我不能这样做。是否还有其他特殊语法可以调用它。

A possible, straight-forward way to do this without comma overloading would be: 在不引起逗号重载的情况下,一种可能的简单方法是:

class Object
{
public:
    template<typename Numbers = std::initializer_list<int>>
    Object &operator[](const Numbers &numbers)
    {
        std::copy(std::begin(numbers), std::end(numbers), std::back_inserter(x_));
        return *this;
    }

private:
    std::vector<int> x_;
};
int main()
{
    Object object;
    auto &obj = object[{1,2,3,4}];
    std::vector<int> numbers = {1, 2, 3, 4};
    obj = obj[numbers];
    return 0;
}

Some noteworthy changes: 一些值得注意的变化:

  • class member visibility 班级成员的知名度
  • operator[] is more generic and returns a reference operator []更通用,并返回引用
  • caller accepts by reference 呼叫者通过引用接受

I understand that you want to use the [] operator in C++ to insert a list of numbers to your object's vector. 我知道您想在C ++中使用[]运算符将数字列表插入对象的向量。

If that's the case, you can make use of C++11's implicit conversion. 如果是这样,您可以利用C ++ 11的隐式转换。 Here's a sample code: 这是一个示例代码:

class Object
{

public:

    Object operator[]( vector<int> vx )
    {
        v.insert( v.end(), vx.begin(), vx.end() );
        return *this;
    }

private:

    vector<int> v;
};

This way you can use: 这样您可以使用:

Object o;
o[{1, 2, 5}];

to insert the list "1, 2, 5" to your object's vector easily. 轻松将列表“ 1、2、5”插入对象的向量。 The {1, 2, 5} will be implicitly converted to a vector of integers and your custom [] operator function will be called and executed. {1,2,5}将隐式转换为整数向量,并且将调用并执行您的自定义[]运算符。

The operator[](int) is a binary operator. operator[](int)是二进制运算符。 It can't be overloaded to take more then 1 index argument. 不能接受超过1个索引参数的重载。

here some working example: 这里是一些工作示例:

#include <iostream>
#include <vector>

using namespace std;

class Object{
  public:
    Object operator[](int temp){
        this->x.push_back(temp);
        return *this;
    }
    vector<int> x;
};

int main() {

    auto x = Object();
    x[1];
    x[2];
    x[3];

   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM