c ++打破类函数 [英] c++ breaks on class function

查看:109
本文介绍了c ++打破类函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这里是我的函数

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;
}


推荐答案

strong>:等待...我还没有意识到这个函数是 CXFileEntity类的成员。它看起来不像是一个静态方法。所以为了调用这个函数,你已经需要实例化一个CXFileEntity对象!因此,很可能你绝对不希望删除或创建此方法的内部中的CXFileEntity对象。 (如果你真的一次只允许一个实体存在,你将有效地删除'this',然后尝试重新创建它。这不工作,没有办法,没有办法。)

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.

但这显然是错误的:

CXFileEntity *entity, 

,因为它意味着由

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.

您还必须更改调用者以将指针传递给指针。为了好的缘故,请确保第一次传递指针时,它初始化为0:

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;
...

这篇关于c ++打破类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆