试图使C 类易于使用指针
Trying to make a c++ class easier to use with pointers
我只是为了学习而尝试使用派生的vector3类做一个向量类。第一个矢量类具有双* V;数组和一些[]运算符的指针,以更轻松的数据访问,vector3还具有x,y,z pointers。
课程的重要部分就像:
class Vector{
protected:
double* v;
int size_;
public:
[ ... a lot of stuff ... ]
double & operator [](int i);
}
class Vector3 : public Vector{
public:
double* x; //Access to v[0]
double* y; //Access to v[1]
double* z; //Access to v[2]
Vector3();
Vector3(double,double,double);
};
所以我的目的是制作这样的代码:
//You can create a Vector3 and access with x, y, z values:
Vector3 v3 = Vector3(10,9,8);
cout << "Testing v3.x -- v3.y -- v3.z" << endl;
cout << v3.x << " -- " << v3.y << " -- " << v3.z << endl;
//And also change its values
v3.x = 5;
v3.y = 1;
v3.z = 6;
//Now, the two following couts should print the same:
cout << "Testing v3.x -- v3.y -- v3.z and v3[0] -- v3[1] -- v3[2]" << endl;
cout << v3.x << " -- " << v3.y << " -- " << v3.z << endl;
cout << v3[0]<< " -- " << v3[1]<< " -- " << v3[2]<< endl;
我的问题是:
可以在不修改最后一个代码的情况下执行此操作?
我知道我可以轻松地使这项工作更改为v3.x [0]或类似的东西,但我希望它更直观。
如果您不需要operator=
Vector3
类,则可以将指针更改为参考。
class Vector3 : public Vector{
public:
double& x; //Access to v[0]
double& y; //Access to v[1]
double& z; //Access to v[2]
Vector3();
Vector3(double,double,double);
};
相关文章:
- 排序阵列会使指针链接错误
- C++ ->尝试逐字阅读一行文本。如何使指针等效于用于存储输入的当前二维数组
- 调整窗口内存映射文件的大小,而不会使指针失效
- 试图使C 类易于使用指针
- 使指针类型围绕其最大值
- 使指针的中间;它有效吗?
- 如何使指针指向模板化成员函数的专用变体
- 使指针常量C++
- 使指针全局C++
- 是否可以在不使指针无效的情况下调整/重新分配大块内存
- 使指针指向数组c++的开头
- 警告:指针和整型之间的比较,传递' read '的参数1使指针的整型没有强制转换
- 如何使指针指向指针NULL
- 如何使指针/引用的元素在向量上
- 使指针Getter使用unique_ptr
- 圆括号使指针模板参数无效
- 如何使指针指向第二个数组中的字符
- 如何使指针指向其祖父母
- 关于指针对齐,有没有一种方法可以使指针与给定的内存边界对齐
- std::move是否使指针无效