C++ 转换为同一个类

标签 c++ casting operator-overloading

我对具有任意上限和下限的数组进行了自定义实现。现在我希望能够在相同类型的数组之间自由转换,只要它们具有相同的长度即可。在代码中它看起来像这样:

template<typename T, int L, int H>
class Array{
    public:
        // stuff ...

        operator Array<T, int lb, int hb>& () {
            auto converted = Array<T, lb, hb>;
            converted.actualArray = actualArray;
            converted.offset = lb;
            return *converted;
        }
    private:
        T actualArray[H - L + 1];
        int offset = 0 - L;
}

如您所见,该类需要转换为自身。正如您可能还看到的那样,我是 C++ 的菜鸟,因为我收到的错误似乎是一个语法错误:

wrong number of template arguments (2, should be 3)
operator Array<T, int lb, int hb>& () {
                                ^

'<expression error>' does not name a type
operator Array<T, int lb, int hb>& () {
^

我做错了什么,无法识别我的运算符的返回类型?我真的希望这不是一个简单的错字,那将是愚蠢的。

最佳答案

运算符本身需要第二组模板参数,因此可以使用与主类使用的模板值不同的模板值来调用它。

即使您可以正确获取模板参数,它仍然无法工作,因为您无法将原始数组分配给另一个原始数组。您需要改为复制元素,例如使用 std:copy()std::copy_n()

尝试更像这样的东西:

#include <algorithm>

template<typename T, size_t L, size_t H>
class Array
{
public:
    static_assert(H >= L);
    static const size_t Low = L;
    static const size_t High = H;
    static const size_t Length = (H - L + 1);

    // stuff ...

    template<size_t Lb, size_t Hb>
    operator Array<T, Lb, Hb>() const
    {
        static_assert(Length == Array<T, Lb, Hb>::Length);
        Array<T, Lb, Hb> converted;
        std::copy_n(actualArray, Length, converted.actualArray);
        return converted;
    }

    // just to show that you don't need an offset member...

    T& operator[](size_t idx)
    {
        return actualArray[idx - L];
    }

    T operator[](size_t idx) const
    {
        return actualArray[idx - L];
    }

    template<typename, size_t, size_t>
    friend class Array;

private:
    T actualArray[Length];
};

Live Demo

或者,您可以定义一个复制构造函数,它接受具有相同数组大小的多个数组类型,然后您就不再需要转换运算符了:

#include <algorithm>

template<typename T, size_t L, size_t H>
class Array
{
public:
    static_assert(H >= L);
    static const size_t Low = L;
    static const size_t High = H;
    static const size_t Length = (H - L + 1);

    // stuff ...

    Array() = default;

    template<size_t Lb, size_t Hb>
    Array(const Array<T, Lb, Hb> &src)
    {
        static_assert(Length == Array<T, Lb, Hb>::Length);
        std::copy_n(src.actualArray, Length, actualArray);
    }

    // just to show that you don't need an offset member...

    T& operator[](size_t idx)
    {
        return actualArray[idx - L];
    }

    T operator[](size_t idx) const
    {
        return actualArray[idx - L];
    }

    template<typename, size_t, size_t>
    friend class Array;

private:
    T actualArray[Length];
};

Live Demo

关于C++ 转换为同一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51033098/

相关文章:

c - isalnum 等效于使用#define

c# - 如何在 C# 中将泛型类转换为泛型接口(interface)

c++ - 将重载运算符 << 与运算符 * 一起使用时出错

c++ - 在 C++ 中查找字符串的所有排列

python - 如何使用 native Python/C API 从 C++ 创建 python 自定义类型?

c++ - 比较两个字符串的最佳或最快方法是什么?

java - 如果从数组中复制最终变量,为什么 Java 需要对它进行显式强制转换?

C++:如何为字符数组创建比较函数?

c++ - 添加这些字符串有什么区别?

c++ - 双向链表插入无限循环... C++