将类互换使用浮动指针
Use class as a float pointer interchangeably
我想知道它是否存在一种互换使用类和浮动指针的方法。假设课程基本上是双打(固定尺寸)的数组。如果我有类指针,我可以将其用作浮动指针(与适当的操作员一起使用),但是,如果我有指针,我不知道如何自动将其用作类。
让我再解释一下我的问题。我一直在使用Matrix4x4 Typedef保存4x4矩阵:
typedef float Matrix4x4[16];
我有很多功能将Matrix4x4
作为float*
现在,我正在尝试以使用Matrix4x4的方式使用基本类:
class Matrix4x4 {
float matrix[16];
public:
Matrix4x4();
float operator[](int i){
return matrix[i];
}
operator float*() const{ // I can pass to functions that take a float*
return (float*) matrix;
}
};
当我需要调用这样的函数时,问题仍然存在:
bool test(void){
float H[16];
// ... process H
return isIdentidy( H); // I want the compiler to accept it this way
return isIdentidy((float*) H); // or this way
}
bool isIdentity(const Matrix4x4 matrix){
... (process)
return ...;
}
在最后,指针应该是相同的吧?
(如果我将h声明为 Matrix4x4 H
,而不是 float H[16]
有没有办法无需使用static_cast或dynamic_cast?
非常感谢
没有办法做你想做的事,但是你可以做一些非常相似的事情。
首先为接受float [16]参数
的Matrix4x4制作新的构造函数class Matrix4x4 {
float matrix[16];
public:
Matrix4x4();
Matrix4x4(float values[16])
{
memcpy(matrix, values, sizeof(float)*16);
}
float operator[](int i){
return matrix[i];
}
operator float*() const{
return (float*) matrix;
}
};
然后您可以做
bool test(void){
float H[16];
// ... process H
return isIdentidy(Matrix4x4(H));
}
bool isIdentity(const Matrix4x4 matrix){
... (process)
return ...;
}
新Matrix4x4的任何更改都将丢失。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 将类互换使用浮动指针
- 仅通过指针强制转换使类类型可互换,而无需分配任何新对象
- 成员函数指针在不同地址空间之间的互换性