1

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?

4

2 回答 2

5

不能直接调用临时地址操作符(MSVC 会告诉您这也不是更高警告级别的标准 C++)。除非你可以

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

但这很恶心。只需声明一个局部变量并传递其地址。

于 2010-03-11T21:38:38.347 回答
0

我认为没有理由在函数调用之前不实例化对象,然后再将其删除。另外值得注意的是,

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

我认为如果 Function 不返回指针或将其存放到某种内存池中,这将导致内存泄漏。希望这可以帮助。

于 2010-03-11T21:52:04.010 回答