Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 静态函数的意外行为_C++_Static - Fatal编程技术网

C++ 静态函数的意外行为

C++ 静态函数的意外行为,c++,static,C++,Static,我在做一个DX11项目,我得到一个静态函数的奇怪行为。我对静态函数有点无知,所以我可能在某个地方犯了一个错误 我的功能很简单: namespace Rendering { static bool PlaceObjectInMatrix(EntityBase* obj, D3DXMATRIX world) { D3DXMatrixTranslation(&world, obj->GetPosition()->x, obj->GetPosi

我在做一个DX11项目,我得到一个静态函数的奇怪行为。我对静态函数有点无知,所以我可能在某个地方犯了一个错误

我的功能很简单:

namespace Rendering
{
    static bool PlaceObjectInMatrix(EntityBase* obj, D3DXMATRIX world)
    {

        D3DXMatrixTranslation(&world, obj->GetPosition()->x, obj->GetPosition()->y, obj->GetPosition()->z); 
        return true;
    }
}
并在循环中调用它以渲染所有对象:

for(unsigned int i = 0; i < listSize; i++)
    {

        if(entities[i].taken == false)
            continue; 
        entities[i].entry->Render(deviceContext, shader);

        PlaceObjectInMatrix(entities[i].entry, worldMatrix);

        if(entities[i].entry->GetIndexCount() != 0) 
        {
            shader->Render(deviceContext, entities[i].entry->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, entities[i].entry->GetTexture(), m_debugLight->GetDirection(), m_debugLight->GetDiffuseColor());
            objectsRendered++;
        }

        d3d->GetWorldMatrix(worldMatrix); //reset world matrix
    }
for(无符号整数i=0;i渲染(deviceContext,着色器);
PlaceObjectInMatrix(实体[i]。条目,worldMatrix);
如果(实体[i].entry->GetIndexCount()!=0)
{
着色器->渲染(deviceContext,实体[i]。条目->GetIndexCount(),worldMatrix,viewMatrix,projectionMatrix,实体[i]。条目->GetTexture(),m_debugLight->GetDirection(),m_debugLight->GetDiffuseColor();
objectsrended++;
}
d3d->GetWorldMatrix(worldMatrix);//重置世界矩阵
}
但是,它不起作用。然而,当我用函数本身的内容替换函数调用的行时,它确实起作用。在函数调用中好像有什么东西丢失了

EntityBase类是抽象的,我实际使用的类称为EntityModelStatic,它派生自EntityModel

static bool PlaceObjectInMatrix(EntityBase* obj, D3DXMATRIX world)
world
参数是按值传递的,因此函数获得自己的副本,并且调用方中的变量不会更改。这可能是你的问题

使用指针(如
D3DXMatrixTranslation
)或引用,您的问题可能会消失

world
参数是按值传递的,因此函数获得自己的副本,并且调用方中的变量不会更改。这可能是你的问题

使用指针(如D3DXMatrixTranslation)或引用,您的问题可能会消失。

PlaceObjectInMatrix()函数在worldMatrix copy上运行。要更改原始矩阵,必须声明如下函数:

static bool PlaceObjectInMatrix(EntityBase* obj, D3DXMATRIX& world);
static bool PlaceObjectInMatrix(const EntityBase& obj, D3DXMATRIX* world);
更传统的方法是使用指针,如下所示:

static bool PlaceObjectInMatrix(EntityBase* obj, D3DXMATRIX& world);
static bool PlaceObjectInMatrix(const EntityBase& obj, D3DXMATRIX* world);
这样,任何阅读调用代码(如PlaceObjectInMatrix(*entities[i].entry,&worldMatrix)的人都会意识到,该函数将更改worldMatrix对象,PlaceObjectInMatrix()函数将对worldMatrix副本进行操作。要更改原始矩阵,必须声明如下函数:

static bool PlaceObjectInMatrix(EntityBase* obj, D3DXMATRIX& world);
static bool PlaceObjectInMatrix(const EntityBase& obj, D3DXMATRIX* world);
更传统的方法是使用指针,如下所示:

static bool PlaceObjectInMatrix(EntityBase* obj, D3DXMATRIX& world);
static bool PlaceObjectInMatrix(const EntityBase& obj, D3DXMATRIX* world);

这样,任何阅读PlaceObjectInMatrix(*entities[i].entry,&worldMatrix)等调用代码的人都会意识到,该函数将更改worldMatrix对象,因为我对Direct3D了解不多,这只是一个建议:将
world
参数设为参考
D3DXMATRIX&world
。由于我对Direct3D了解不多,这只是一个建议:将
world
参数设为参考
D3DXMATRIX&world