c++破坏类功能

发布于 2024-10-01 10:09:31 字数 596 浏览 5 评论 0原文

我已经创建了这个类来进行网格加载,它可以工作,但是我添加了这个新函数来帮助加快加载速度。当我调用该函数时,我的程序中断/停止。
这是我的功能

bool CXFileEntity::LoadXFile(const std::string &filename, int startAnimation, CXFileEntity *entity, LPDIRECT3DDEVICE9 d3ddev)
{
    // We only support one entity so if it already exists delete it
    if (entity)
    {
        delete entity;
        entity=0;
    }

    // Create the entity
    entity=new CXFileEntity(d3ddev);
    if (Load(filename))
    {
        delete entity;
        entity=0;
        return false;
    }

    SetAnimationSet(startAnimation);

    return true;
}

i have created this class for mesh loading it works but i added this new function to help speed up the loading. when i call the function my program breaks/stops.
here is my function

bool CXFileEntity::LoadXFile(const std::string &filename, int startAnimation, CXFileEntity *entity, LPDIRECT3DDEVICE9 d3ddev)
{
    // We only support one entity so if it already exists delete it
    if (entity)
    {
        delete entity;
        entity=0;
    }

    // Create the entity
    entity=new CXFileEntity(d3ddev);
    if (Load(filename))
    {
        delete entity;
        entity=0;
        return false;
    }

    SetAnimationSet(startAnimation);

    return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-10-08 10:09:31

编辑:等等...我没有意识到这个函数是CXFileEntity类的成员。它看起来也不像是一个静态方法。因此,为了调用此函数,您已经需要实例化一个 CXFileEntity 对象!因此,您可能绝对不想在此方法的内部删除或创建 CXFileEntity 对象。 (如果你真的一次只允许一个实体存在,那么你将有效地删除“这个”,然后尝试重新创建它。这是行不通的,没办法,没有办法。)

我要离开较早的答案希望它仍然能为您提供有关指针如何工作的一些线索。


您最好提供更多信息,例如程序中断的位置如何

但这显然是错误的:

CXFileEntity *entity, 

by 分配的新对象

entity=new CXFileEntity(d3ddev);

因为这意味着调用者将看不到 。 (实体是局部变量,因此在局部范围之外不会看到对其进行的更改。)

尝试更改代码以将实体作为指向指针的指针传递:

CXFileEntity **entity, 

内的代码以匹配:

if (*entity)
{
    delete *entity;
    *entity=0;
}

// Create the entity
*entity=new CXFileEntity(d3ddev);

// etc.

这意味着更改函数 还必须更改调用者以将指针传递给指针。看在上帝的份上,请确保第一次传入指针时,它被初始化为 0:

CXFileEntity *the_entity = 0;
...

EDIT: Wait... I hadn't realized that this function is a member of the CXFileEntity class. It doesn't look like it's a static method, either. So in order to call this function, you already need to have instantiated a CXFileEntity object! Therefore, it's likely that you absolutely do not want to be either deleting or creating CXFileEntity objects inside of this method. (If you truly only allow one entity to exist at a time, you'll be effectively deleting 'this' and then trying to re-create it. That doesn't work, no way, no how.)

I'm leaving the earlier answer in place in hopes that it will still provide you some clue about how pointers work.


You'd do better to give more information, such as where and how the program breaks.

But this is clearly wrong:

CXFileEntity *entity, 

because it means that the new object allocated by

entity=new CXFileEntity(d3ddev);

will not be seen by the caller. (entity is a local variable, so changes to it won't be seen outside of the local scope.)

Try changing the code to pass entity as a pointer to a pointer:

CXFileEntity **entity, 

which will mean changing the code inside the function to match:

if (*entity)
{
    delete *entity;
    *entity=0;
}

// Create the entity
*entity=new CXFileEntity(d3ddev);

// etc.

You'll also have to change the caller to pass a pointer to a pointer. And for goodness' sake, make sure that the first time you pass the pointer in, it's initialized to 0:

CXFileEntity *the_entity = 0;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文