用于多维数组的 C++ const_cast 运算符

标签 c++ arrays c++11 const-cast

我正在寻找一种在 C++11 中封装重载 const_cast 的方法定义成员操作的结构/类的多维数组成员的运算符。我在 SO 上搜索过,但我真的找不到下面描述的问题的答案。

具体来说,我正在处理在第三方 C API 中定义为 typedef 的 4x4 矩阵。在 double[4][4] . API 还提供使用此矩阵的函数。

API 如下所示:

typedef double ApiMatrix[4][4];

bool ApiMatrixFunction(ApiMatrix matrix)
{
    // .. some code
    return true;
}

我已经实现了结构 MyMatrix封装对此数据类型的操作:

struct MyMatrix
{
private:
//public:
    double m[4][4];

public:

    MyMatrix() { ... }

    // lots of operations and initialization members
    ....

    // overloading typical calculation operators +,-,*, ....
    ....

    // cast operator to the API data type
    operator ApiMatrix& ()
    {
        return m;
    }
};

这在使用 MyMatrix 时效果很好作为引用(MyCodeWithRef),但将其用作常量引用(MyCodeWithConstRef)会很麻烦。可能的解决方法是复制函数中的变量或授予对私有(private)数据的访问权限并通过 const_cast<double(*)[4]>(matrix.m) 将其强制转换到位。 .

// Using MyMatrix reference
void MyCodeWithRef(MyMatrix& matrix)
{
    ApiMatrixFunction(matrix);
}

// Using MyMatrix constant reference
void MyCodeWithConstRef(const MyMatrix& matrix)
{

    // Unfortunately, this fails
    ApiMatrixFunction(matrix);

    // workaround 1: copy the matrix
    MyMatrix m = matrix;

    // workaround 2: use a cast operator in this function
    // requires access to the private m.
    ApiMatrixFunction(const_cast<double(*)[4]>(matrix.m));
}

这两种解决方法都有明显的缺点,我正在寻找一种方法来定义 const_cast MyMatrix 中的运算符结构,因此我可以对 const 和非常量引用使用相同的调用。

根据评论添加:

为了更具体地回答我的问题,我想添加一种自定义 const_cast接线员到 MyMatrix .类似于以下内容(当然不起作用):

operator const ApiMatrix& () const
{
   return const_cast<double(*)[4]>(m);
}

最佳答案

选项 A:

typedef double ApiMatrix[4][4];

class Matrix {
    ApiMatrix a;
public:
    operator ApiMatrix&() const {
        return const_cast<ApiMatrix&>(a);
    }
};

bool ApiMatrixFunction(ApiMatrix matrix)
{
    // .. some code
    return true;
}

// Using MyMatrix reference
void MyCodeWithRef(Matrix& matrix)
{
    ApiMatrixFunction(matrix);
}

// Using MyMatrix constant reference
void MyCodeWithConstRef(const Matrix& matrix)
{
    ApiMatrixFunction(matrix);
}

选项 B:

typedef double ApiMatrix[4][4];

class Matrix {
    ApiMatrix a;
public:
    operator const ApiMatrix&() const {
        return a;
    }
};

bool ApiMatrixFunction(ApiMatrix matrix)
{
    // .. some code
    return true;
}

// Using MyMatrix reference
void MyCodeWithRef(Matrix& matrix)
{
    const ApiMatrix& a = matrix;
    ApiMatrixFunction(const_cast<ApiMatrix&>(a));
}

// Using MyMatrix constant reference
void MyCodeWithConstRef(const Matrix& matrix)
{
    const ApiMatrix& a = matrix;
    ApiMatrixFunction(const_cast<ApiMatrix&>(a));
}

关于用于多维数组的 C++ const_cast 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45098509/

相关文章:

arrays - 检查 Postgres 中的数组中是否存在多个值中的任何一个

Javascript:过滤数组中的元素部分

python - 使用下面代码中的 "i-=smallest"语句,我打算更改原始数组 arr,但这并没有发生。我该怎么办?

c++ - 具有已删除析构函数的类型的动态对象是如何构造的?

c++ - QRegExp 提取数组名和索引

c++:流输出操作成功但状态不好

c++ - 如何将字符串 vector 中的每个单词更改为大写

c++ - 多线程 vector

c++ - 对 std::string 数组进行二进制搜索

c++ - 如何在 mac 上的 Eclipse CDT 上编译 C++0x 代码?