c++ - 返回泛型函数中对象的引用(具有指针或非指针属性)

标签 c++ pointers generics constants

--大学作业--

我已经使用过如下所示的通用函数:

template<class T1, class T2, int max>
class Collection{
    T1* _elements1[max];
    T2* _elements2[max];
    int _count;
public:
    // ctor

    bool AddElement(const T1& e1, const T2& e2)
    {
        for (int i = 0; i < _count; i++)
            if (_elements1[i] == e1 && _elements2[i] == e2)
                return false;

        _elements1[_count] = e1;
        _elements2[_count] = e2;

        _count++;

        return true;
    }

    int GetMax()const { return max;}
    T1& GetElement1(int index)const { return *_elements1[index]; }
    T2& GetElement2(int index)const { return *_elements2[index]; }
}

main() :

Collection<int, double, 6> collection;

for (int i = 0; i < 6; i++)
    collection.AddElement(i, i + 0.4);

cout << collection << endl;

我也用constoperator<<对于这个类,一切都很好,没有与 const 相关的编译器投诉.

但是,今天我尝试了这门课的稍微不同的版本,我们应该练习它,因为它将以某种形式出现在考试中。

template<class T1, class T2, int max>
class Collection{
    T1 _elements1[max];
    T2 _elements2[max];
    int _count;
public:
    // ctor

    int GetMax()const { return max;}
    T1& GetElement1(int index)const { return _elements1[index]; }
    T2& GetElement2(int index)const { return _elements2[index]; }
}

这里的区别在于T1T2不是 pointers 的数组但是当 return 时,也是普通对象的数组,并且也在底部ing,没有必要取消引用。

但是,在后一个示例中,我遇到了这个错误:

error C2440: 'return' : cannot convert from 'const int' to 'int &'

虽然我用谷歌搜索了一下,发现我是否放了另一个 const在这些函数前面是这样的:

const T1& GetElement1(int index)const { return _elements1[index]; }
const T2& GetElement2(int index)const { return _elements2[index]; }

错误消失了。

当然,这解决了我的问题,但我更愿意了解为什么会发生这种情况以及幕后到底发生了什么。如果有一种简单的方法可以解释我提供的两个示例之间的区别,我们将不胜感激。

最佳答案

在第一个 Collection 版本中,_elements1[index] 的类型是 int *const&。这个const是因为你在const方法中访问了_elements1(实际上thisconst在这种情况下),这也意味着您不能修改 _elements1[index]。当您取消引用 _elements1[index] 时,您将得到 int&,这就是 _elements1[index] 指向的内容。因此,您可以从返回 T1& 的 GetElement1 返回它。

现在进入您的第二个 Collection 类。这里 _elements1[index]GetElement1 方法中是 const int& 类型,因为你在 const 方法中访问 _elements1,所以添加了 const。现在你的错误的原因是因为你不能隐式地将 const int& 转换为 int&

如果你想知道什么类型在不同的地方有_elements1[index],你可以使用下面的技巧:

template<typename T>
struct TD;

// ...

T1& GetElement1(int index)const { 
    TD<decltype(_elements1[index])> tt;        
    return _elements1[index]; 
    }

将输出 decltype 上面的确切类型的错误:

error: implicit instantiation of undefined template 'TD<const int &>'
                                                        ^^^^^^^^^^^ - type of _elements1[index]

关于c++ - 返回泛型函数中对象的引用(具有指针或非指针属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37280238/

相关文章:

c++ - 他们为什么不添加 iota 的运营商版本?

c++ - Jsoncpp JSON :Reader causing error "Debug assertion... _pFirstBlock == pHead"

c++ - 使用前导下划线真的会造成麻烦吗?

generics - 使用 Rust 空类型作为通用绑定(bind)

Java泛型编译错误 "Class<capture#1-of ? ..."

c++ - 如果启用换行,如何获取第一个显示的文档行号?

c - malloc() 函数的返回

c - 将 token 指针(来自 strtok)分配给结构的 2d VLA 的指针元素时出现段错误

c++ - 如何在 C++ 中将 int[][] 转换为 int**

android - 如何使用 kotlin 在构造函数中创建传递方法引用的泛型类实例