如何检查是否已在C ++中初始化/创建了对象?

时间:2023-01-20 21:21:25

So I have the following class...

所以我有以下课程......

class Pet
{
    public:
        Pet() : id(0),
            name("New Pet")
        {

        }

        Pet(const int new_id, const std::string new_name) : id(new_id),
            name(new_name)
        {

        }

        Pet(const Pet new_pet) : id(new_pet.id),
            name(new_pet.name)
        {

        }
    private:
        const int id;
        const std::string name;
};

Somewhere in my code I then create a instance of this class like so...

在我的代码中的某处,然后我创建了这个类的实例,就像这样......

Pet my_pet = Pet(0, "Henry");

Later on in my code, an event is supposed to cause this pet to be deleted. delete(my_pet);

稍后在我的代码中,一个事件应该导致该宠物被删除。删除(my_pet);

How do I check if my_pet has been initialized...

如何检查my_pet是否已初始化...

Would something like this work?

会这样的吗?

if(my_pet == NULL)
{
    // Pet doesn't exist...
}

2 个解决方案

#1


Assuming you mean

假设你的意思

Pet* my_pet = new Pet(0, "Henry");

instead of Pet my_pet = Pet(0, "Henry");
You can initialise your Pet object to NULL (or nullptr for C++11) like so:

而不是宠物my_pet =宠物(0,“亨利”);您可以将Pet对象初始化为NULL(或C ++ 11的nullptr),如下所示:

Pet* pet = NULL; // or nullptr

and later assign it an instance of Pet:

然后为它分配一个Pet实例:

pet = new Pet(0, "Henry");

This allows you to check the value of pet without invoking undefined behaviour (through uninitialised variables):

这允许您在不调用未定义行为的情况下检查pet的值(通过未初始化的变量):

if (pet == NULL) // or nullptr
{
    ...
}

#2


delete(my_pet); doesn't make sense, neither by the signature used (should be delete my_pet;, if valid).

删除(my_pet);没有意义,也没有使用的签名(应删除my_pet;,如果有效)。

With your code

用你的代码

Pet my_pet = Pet(0, "Henry");

no dynamic memory allocation was involved, thus you don't ever need to call delete my_pet;

没有涉及动态内存分配,因此您不需要调用delete my_pet;

The object instance will be destroyed as soon the scope where you called Pet my_pet = Pet(0, "Henry"); is left.

在您调用Pet my_pet = Pet(0,“Henry”)的范围内,对象实例将被销毁;离开了。


As for your comment "How would I go about forcing the deletion of the pet.", you should use dynamic memory management smart pointers, rather calling new Pet() and bothering about forcing deletion yourself.

至于你的评论“我将如何强制删除宠物。”,你应该使用动态内存管理智能指针,而不是调用新的Pet()并为自己强制删除而烦恼。

If you really need dynamic memory allocation for Pet, rather use something like

如果你真的需要动态内存分配给宠物,而是使用像

std::unique_ptr<Pet> my_pet(new Pet(0, "Henry"));`

#1


Assuming you mean

假设你的意思

Pet* my_pet = new Pet(0, "Henry");

instead of Pet my_pet = Pet(0, "Henry");
You can initialise your Pet object to NULL (or nullptr for C++11) like so:

而不是宠物my_pet =宠物(0,“亨利”);您可以将Pet对象初始化为NULL(或C ++ 11的nullptr),如下所示:

Pet* pet = NULL; // or nullptr

and later assign it an instance of Pet:

然后为它分配一个Pet实例:

pet = new Pet(0, "Henry");

This allows you to check the value of pet without invoking undefined behaviour (through uninitialised variables):

这允许您在不调用未定义行为的情况下检查pet的值(通过未初始化的变量):

if (pet == NULL) // or nullptr
{
    ...
}

#2


delete(my_pet); doesn't make sense, neither by the signature used (should be delete my_pet;, if valid).

删除(my_pet);没有意义,也没有使用的签名(应删除my_pet;,如果有效)。

With your code

用你的代码

Pet my_pet = Pet(0, "Henry");

no dynamic memory allocation was involved, thus you don't ever need to call delete my_pet;

没有涉及动态内存分配,因此您不需要调用delete my_pet;

The object instance will be destroyed as soon the scope where you called Pet my_pet = Pet(0, "Henry"); is left.

在您调用Pet my_pet = Pet(0,“Henry”)的范围内,对象实例将被销毁;离开了。


As for your comment "How would I go about forcing the deletion of the pet.", you should use dynamic memory management smart pointers, rather calling new Pet() and bothering about forcing deletion yourself.

至于你的评论“我将如何强制删除宠物。”,你应该使用动态内存管理智能指针,而不是调用新的Pet()并为自己强制删除而烦恼。

If you really need dynamic memory allocation for Pet, rather use something like

如果你真的需要动态内存分配给宠物,而是使用像

std::unique_ptr<Pet> my_pet(new Pet(0, "Henry"));`