Jump to content
  • Advertisement
Sign in to follow this  
Kobalt64

ffs, Pointers.

This topic is 4139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!