• Vector与数组的转化


    可以重载*运算符

    1 operator float *(){ return &_x ;} ;
    2 operator const float *() constreturn &_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 ;
     }

    ==

  • 相关阅读:
    Codeforces 1230E
    扫描线求面积和周长 (模板,线段树)
    POJ2528
    线段树 (模板)
    Codeforces 1332B
    Codeforces 1279D
    Codeforces 1281B
    Codeforces 1288D
    8I
    8F
  • 原文地址:https://www.cnblogs.com/graphics/p/1680573.html
Copyright © 2020-2023  润新知