Hello all,
I'm currently working on a 3D graphic engine but I'm stuck with a
small problem on references of pointers.
Some of my c++ class are complex and use many pointers so I decided to
force initialisation of objects through their static function Create,
which has a parameter IN-OUT that will contain the instance of the
created object.
Here is a sample that speaks better than words :
--------------------------------------------------------------------------
typedef class Texture* PTEXTURE;
// With this, PTEXTURE& is a reference of a pointer to Texture
class Texture:
{
protected:
Texture( void ){} // Constructor, do nothing, use Create
public:
static void Create( PTEXTURE& ); // PTEXTURE& is a reference of a
pointer to the texture to create
// Many other stuff that is not important here
};
// Implementation of the Create Function
void Texture::Create ( PTEXTURE& pTexToCreate )
{
pTexToCreate = new Texture; // memory alloc
// Some init...
if( badInit )
{
delete pTexToCreate;
pTexToCreate = NULL;
}
}
// A small example of how I use this
void main( void )
{
PTEXTURE pMyTexture;
Texture::Create ( pMyTexture );
if( pMyTexture == NULL )
// Error creating the texture
}
--------------------------------------------------------------------------
Hope this code is clear enough ;)
Then there is my problem : pMyTexture will never be NULL!! even if
there IS a problem in the Create function and the line "pTexToCrea te =
NULL" is executed by the program. Using the debugger shows that
pTexToCreate is NULL, however when going out of the function,
pMyTexture has a random value pointing to nothing with sense.
I really don't understand that, because using references should affect
the original value passed as a parameter (which is true because when
initialization is successfull, pMyTexture is pointing to a valid
Texture). And I don't want to use pointers of pointers to solve this
problem.
Can anyone help please ? I just would like pMyTexture to be NULL when
Create fails :o/
Vianney
I'm currently working on a 3D graphic engine but I'm stuck with a
small problem on references of pointers.
Some of my c++ class are complex and use many pointers so I decided to
force initialisation of objects through their static function Create,
which has a parameter IN-OUT that will contain the instance of the
created object.
Here is a sample that speaks better than words :
--------------------------------------------------------------------------
typedef class Texture* PTEXTURE;
// With this, PTEXTURE& is a reference of a pointer to Texture
class Texture:
{
protected:
Texture( void ){} // Constructor, do nothing, use Create
public:
static void Create( PTEXTURE& ); // PTEXTURE& is a reference of a
pointer to the texture to create
// Many other stuff that is not important here
};
// Implementation of the Create Function
void Texture::Create ( PTEXTURE& pTexToCreate )
{
pTexToCreate = new Texture; // memory alloc
// Some init...
if( badInit )
{
delete pTexToCreate;
pTexToCreate = NULL;
}
}
// A small example of how I use this
void main( void )
{
PTEXTURE pMyTexture;
Texture::Create ( pMyTexture );
if( pMyTexture == NULL )
// Error creating the texture
}
--------------------------------------------------------------------------
Hope this code is clear enough ;)
Then there is my problem : pMyTexture will never be NULL!! even if
there IS a problem in the Create function and the line "pTexToCrea te =
NULL" is executed by the program. Using the debugger shows that
pTexToCreate is NULL, however when going out of the function,
pMyTexture has a random value pointing to nothing with sense.
I really don't understand that, because using references should affect
the original value passed as a parameter (which is true because when
initialization is successfull, pMyTexture is pointing to a valid
Texture). And I don't want to use pointers of pointers to solve this
problem.
Can anyone help please ? I just would like pMyTexture to be NULL when
Create fails :o/
Vianney
Comment