可以重载*运算符
1 operator float *(){ return &_x ;} ;
2 operator const float *() const{ return &_x ; } ;
3
2 operator const float *() const{ return &_x ; } ;
3
仿照D3D库中的例子,写出如下代码
#include <iostream> using namespace std ; struct Vector { Vector(float x, float y, float z):_x(x), _y(y), _z(z){} Vector(float *p):_x(p[0]), _y(p[1]), _z(p[2]){} operator float *(){ return &_x ;} ; //operator const float *() const{ return &_x ; } ; float _x ; float _y ; float _z ; } ; void OutputVector(Vector &v) { cout << v._x << ", " << v._y << ", " << v._z << endl ; } void OutputArray(float *p) { cout << p[0] << ", " << p[1] << ", " << p[2] << endl ; } int main(void) { // array -> vector float p[] = { 1, 2, 3} ; Vector v(p) ; OutputVector(v) ; // vector -> array Vector v1(2, 3, 4) ; float *p1 = v1 ; OutputArray(p1) ; system("pause") ; return 0 ; }
==