在c ++中使用[]而不是函数调用

时间:2015-08-20 05:08:53

标签: c++ arrays templates

在课堂上,我们被要求写一部分代码来做一些老师写的工作。问题是,我不认识语法。我问过一位朋友,他说这可能是一个超载的运营商,但我不知道该去哪里。

    templateClass < int > obj(array, arrayS);                   

    cout << obj[1] << endl; 

所以它似乎应该输出数组obj的插槽1将保持,但正如我所说,我从来没有见过这在没有()和参数之前使用过。

这会重载[]运算符吗?它如何应用于整个对象?

谢谢。

3 个答案:

答案 0 :(得分:3)

templateClass < int > obj(array, arrayS);int obj(5);的语法相同。

这意味着我们正在声明一个对象obj。类型为templateClass<int>,初始值设定项为arrayarrayS,最终将作为类构造函数的参数。

obj[1]表示在operator[]上调用重载的obj。要查看其实际功能,您需要查找templateClass的类定义。 (或者如果你应该让obj[1]工作,你需要在operator[]的类定义中写一个重载的templateClass

答案 1 :(得分:0)

以下面的课程

为例
template <typename T>
class SimpleArray {
public:
    SimpleArray(const T& first_in = T(), const T& second_in = T()) :
        first(first_in), second(second_in) { }

    T& get(int index) {
        return (index == 0) ? (first) : (second);
    }

private:
    T first;
    T second;
};

并假设主要有

int main() {
    SimpleArray<int> simple(1,2);
    cout << simple.get(0) << ' ' << simple.get(1) << endl;

    return 0;
}

所以这只是创建一个'SimpleArray'类型的对象,Tint(将1和2作为其构造函数的参数传递。然后你只需要进行get()次调用在一个简单的三元运算符的帮助下返回所要求的元素。现在你可以重载[]运算符

template <typename T>
class SimpleArray {
public:
    SimpleArray(const T& first_in = T(), const T& second_in = T()) :
        first(first_in), second(second_in) { }

    T& get(int index) {
        return (index == 0) ? (first) : (second);
    }

    T& operator[] (int index) {
        return this->get(index);
    }

private:
    T first;
    T second;
};

并在主

中测试
int main() {
    SimpleArray<int> simple(1,2);
    cout << simple.get(0) << ' ' << simple.get(1) << endl;
    cout << simple[0] << ' ' << simple[1] << endl;

    return 0;
}

再次在此处,您只需在每次使用get()语法访问数组元素时调用[]函数

答案 2 :(得分:0)

我读了这样的作业:

编写一个接受这些行的模板类:

templateClass < int > obj(array, arrayS);
cout << obj[1] << endl;

第一行是一个构造函数,它接受参数,int作为模板类型,第二行是[]运算符。

但是对模板的功能没有特定要求。换句话说,只要构造函数接受两个参数(和模板类型为int)并且模板类提供[]运算符,您就可以使用您的想象力并实现您喜欢的任何内容。

查看变量名称(数组,数组),老师希望您传递一个数组和数组的大小。因此,templateClass的一个想法可能是将c样式数组作为输入并将值存储到某个c ++容器中的类。

可以这样做:

template <typename T>
class templateClass {
public:
    // Constructor taking array T[] and size as arguments (first requirement)
    templateClass(const T d[], const size_t size)
    {
        for (int i=0; i < size; i++)
        {
            // Copy data from c-style array to a c++ vector 
            mData.push_back(d[i]);
        }
    }

    // [] operator (second requirement)
    T& operator[] (int index)
    {
        if ((index >= mData.size()) || (index < 0))
        {
            // Error - out of range
            // You could throw an exception here or some other error handling
            throw std::invalid_argument( "Invalid index" );
        }
        return mData[index];
    }

private:
    std::vector<T> mData;
};

int main()
{
    try
    {
        int arr[3] = {5, 4, 9};
        templateClass < int > obj(arr, sizeof(arr)/sizeof(arr[0]));

        cout << obj[0] << endl;
        cout << obj[1] << endl;
        cout << obj[2] << endl;

        // Invalid index - will throw exception
        cout << obj[3] << endl;
    }
    catch(...)
    {
        cout << "Exception....";
    }

    return 0;
}
相关问题