ffs, Pointers.

Started by
1 comment, last by K_64 17 years, 2 months ago
Hey everybody, I recently started using C++ again after using C# for ages, and I can't seem to figure something out. I'm building a Rendering Engine, and the main object (RenderableObject) contains pointers to Position, Scale & Rotation Vector's (So the 'User' can modify these Positions/etc outside of the Rendering Engine and have them update automatically). That works fine, I can manually make 3 D3DXVector3's and pass them to the RenderableObject constructor. Problem is, I made a 'TestObject' class (not part of the rendering engine) which contains Position/Scaling/Rotation Vectors aswell as a RenderableObject... Heres the code:

#pragma once

#include "Effect.h"
#include "Mesh.h"
#include "RenderableObject.h"

class TestObject
{
public:
    TestObject( AEngine::Mesh* mesh, AEngine::Effect* effect,
                D3DXVector3 position, D3DXVector3 rotation, D3DXVector3 scale)
    {
        mPosition   = position;
        mRotation   = rotation;
        mScale      = scale;

        mRenderObject = AEngine::RenderableObject(mesh, effect, &mPosition, &mRotation, &mScale);
    }
    ~TestObject(void) 
    { 

    }

    void Update() 
    {
        // Just something to Test if mRenderObject is in-sync 
        mRotation.x += 0.001f;
        mRotation.y += 0.001f;
        mRotation.z += 0.001f;
    }
    
    void Render()
    {
        mRenderObject.Render();
    }

private:

    D3DXVector3                 mPosition;
    D3DXVector3                 mRotation;
    D3DXVector3                 mScale;
    AEngine::RenderableObject   mRenderObject;
};

I then have this code in Main (Aswell as the code to Render them all, ofcourse)...

for(float x = -5; x <= 5; x += 1)
{
    objectList.push_back(TestObject(&mesh, &effect,
                                    D3DXVector3(x,0,0),
                                    D3DXVector3(0,0,0),
                                    D3DXVector3(1,1,1)));
}
The problem is, TestObject.mPosition has the right Data (-5,-4,-3...5), but TestObject.mRenderObject.mPosition always points to 5 (not negative)... Wtf? Thanks for any help.
Advertisement
// temporary object TestObject is created here then a copy is passes to objectList,// when this copy is made the pointers stored by mRenderObject are invalidated// because they will still point to the location in memory (somewhere on the stack).objectList.push_back(TestObject(&mesh, &effect,                                D3DXVector3(x,0,0),                                D3DXVector3(0,0,0),                                D3DXVector3(1,1,1)));


what you need to do to fix this is write both a copy constructor and an assignment operator so your final class looks something like this...

class TestObject{public:    // there is no reason to take pointers to the mesh and effect you should    // use references as a safer more user friendly version (no need for users    // of the class to take the address of the object the class can take a     // reference then take the address of the reference if it needs to store a pointer     TestObject(AEngine::Mesh& mesh, AEngine::Effect& effect,                D3DXVector3 position, D3DXVector3 rotation, D3DXVector3 scale):    // in C++ you should use initializer lists to initialize things in constructors    mPosition(position), // calls mPositions constructor with position as an argument    mRotation(rotation), // ...    mScale(scale), // ...    // calls mRenderObjects constructor with the given arguments    // mRender Objects ctor should be changed to take mesh, effect and arguable      // mPosition, mRotation, and mScale by reference then take the address    // when it stores the pointer    mRenderObject(mesh, effect, mPostition, mRotation, mScale)    {    }    // copy constructor takes const ref to object and makes a copy    friend TestObject(const TestObject& rhs):    mPosition(rhs.mPosition),    mRotation(rhs.mRotation),    mScale(scale),    // pass variables of this object so references are to the new object     // instead of the old    mRenderObject(rhs.mRenderObject.Mesh(), rhs.mRenderObject.Effect(), mPostition, mRotation, mScale)    {    }    friend TestObject& operator=(const TestObject& rhs)    {        // same type of thing as copy ctor        return *this // to allow for TestObject1 = TestObject2 = TestObject3    }    ~TestObject(void)     {     }    void Update()     {        // Just something to Test if mRenderObject is in-sync         mRotation.x += 0.001f;        mRotation.y += 0.001f;        mRotation.z += 0.001f;    }        void Render()    {        mRenderObject.Render();    }private:    D3DXVector3                 mPosition;    D3DXVector3                 mRotation;    D3DXVector3                 mScale;    AEngine::RenderableObject   mRenderObject;};


hope that helps and if theres anything that i said your unsure about just ask.
Thanks Julian, thats all the help I needed, Rate++ [smile].

This topic is closed to new replies.

Advertisement