zoukankan      html  css  js  c++  java
  • 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 ;
     }

    ==

  • 相关阅读:
    VS自带的诊断工具
    Electron学习
    PC跨*台
    .NET调试学习
    Mac使用
    SSL/TLS
    UKey学习
    授权机制OAuth、JWT
    代理服务器
    .NET相关源码查找
  • 原文地址:https://www.cnblogs.com/graphics/p/1680573.html
Copyright © 2011-2022 走看看