CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Typecasting

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Typecasting

    I have a situation where I have two identical storage containers:

    Code:
    ////////////// multiplatform version
    union _SOVector3
    {
        struct { float x, y, z; };
        struct { float r, g, b; };
        float v[3];
    };
    typedef union _SOVector3 SOVector3;
    
    ////////////// GLKit version version
    union _GLKVector3
    {
        struct { float x, y, z; };
        struct { float r, g, b; };
        float v[3];
    };
    typedef union _GLKVector3 GLKVector3;
    SOVector3 is part of a namespace with specialized functions that are generic and intended for multiplatform usage.

    GLKVector3 is dedicated to the Mac and has its own set of functions.

    But what I want to do is freely interchange the storage between these two namespaces. Such as like this:

    Code:
        start = clock();
        SOVector4 myVec4 = SOVector4Make(1.0f, 3.0, 6.0f, 1.0);
        SOMatrix4 myMat4 = SOMatrix4Identity;
    
    
        for (uint i=0; i<100000; ++i ) {
            GLKVector4 result = GLKMatrix4MultiplyVector4((GLKVector4)myVec4, (GLKMatrix4)myMat4);
        }
        end = clock();
        diff = difftime(end, start) / CLOCKS_PER_SEC;
        printf("GLK time to run: %f\n",diff);
    But I am getting errors when I typecast this. Wonder if any c++ experts might have some advice.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Typecasting

    Quote Originally Posted by kloveridge View Post
    I have a situation where I have two identical storage containers:
    Since these are unique structs, but the contents just appear to be the same, then create a template function instead of casting, and inside that function, you work strictly with the contents of the object that's passed.
    Code:
    template <typename T1, typename T2>
    GLKVector4 GLKMatrixMultiplyVector4(T1& vec, T2& mat)
    {
       //...
    }
    //...
    GLKVector4 result = GLKMatrix4MultiplyVector4(myVec4, myMat4);
    Now it doesn't matter what you pass, as long as what is passed satisfies the code in the template function. The T1 and T2 in your example would be SQVector4 and SQMatrix4, respectively.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 27th, 2012 at 06:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured