views:

111

answers:

2

I have a function that takes a const D3DVECTOR3 *pos, but I have no reason to declare this beforehand. The most logical solution to me was using new:

Function(
    //other parameters,
    new D3DXVECTOR3(x, y, 0));

but I don't know how I would go about deleting it, beign intitialized in a function. My next thought was to use the & operator, like so:

Function(
    //other parameters,
    &D3DVECTOR3(x, y, 0));

but I don't know if this is a valid way to go about doing this. (It doesn't get an error, but there are a lot of things that don't give errors that aren't necassarily good). So should I use new, &, or some other technique I'm overlooking?

+5  A: 

It's not possible to directly invoke the address-of operator to the temporary (MSVC will tell you that this is not Standard C++ at higher warning levels, too). Except you may do

Function(
//other parameters,
&(D3DXVECTOR3 const&)D3DXVECTOR3(x, y, 0));

But this is disgusting. Just declare a local variable and pass its address.

Johannes Schaub - litb
Keand64
Johannes Schaub - litb
A: 

I see no reason to not instantiate the object before the function call then delete it afterwards. Also worth noting,

Function(
//other parameters,
new D3DXVECTOR3(x, y, 0));

I think this will result in a memory leak if Function does not return a pointer or deposit it into some kind of memory pool. Hope this helps.

Brandon