void pointer and dynamic memory help

This is a discussion on void pointer and dynamic memory help within the C++ Programming forums, part of the General Programming Boards category; hi im using VC++.net and writing a 3d game engine with directx 8.1, however i have run into a problem ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    void pointer and dynamic memory help

    hi im using VC++.net and writing a 3d game engine with directx 8.1, however i have run into a problem which i cant find a solution to. (this is a simple problem and has nothing to do with directx or 3d graphics theory)

    this is my problem, i have a void pointer which is initalized to be an array of 4 structs, however depending on the flow of the program it is not always intialized into the same type.

    this is an example:

    //the vertex pointer whos type is set by eather having lighting or not
    void * vertices;


    //if there is lighting
    //define vertex type for light
    reinterpret_cast <VertexLitTex *> ( vertices) = new VertexLitTex[ 4 ];


    //if there is no lighting
    //no normals are needed
    reinterpret_cast <VertexTex *> ( vertices ) = new VertexTex[ 4 ];

    then later on in the constructor i go on to set the values of the structs, it must be done this way. this is where i get the problem
    the error is: "error C2106: '=' : left operand must be l-value".
    i know what the problem is and what is wrong, i just dont know the solution.

    what i want to say is basicly this:
    ...
    vertices[ 0 ].x = -half_height;
    vertices[ 0 ].y = half_height;
    ...
    i have tried this and other varients but they all yield the same error:

    reinterpret_cast <VertexLitTex *> ( vertices[ 0 ].x &nbsp = half_height;

    thank you for your time,
    your help is greatly appreciated!

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I can think of one way to solve the problem...

    Create a base class Vertex, and derive VertexTex and VertexLitTex from it... then instead of using a void * you can use a Vertex *. The use a dynamic_cast<> one you determine which class you'll need to use.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    thank you man,
    i never thought about that,
    i will try it.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    i have thought about that, and i dont belive it will work. because it "downcasts" a Vertex class to a VertexLitTex class, because VertexLitTex is the derived class from Vertex:
    Code:
    class Vertex
    {
    public:
         float x, y, z;
         DWORD color;
    };
    
    class VertexLitTex : public Vertex
    {
    public:
         float nx, ny, nz;  //vertex normals
         float u, v;           //texture coords
    };

    well time is my enemy here, this projects deadline is fastly approching, so i belive i will just make two arrays and use wichever. -its dirty but it will do the job

    thanks again man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21