How do I check if an Object has been initialized/created in C++?
Asked Answered
R

3

5

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

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

Would something like this work?

if(my_pet == NULL)
{
    // Pet doesn't exist...
}
Rosenstein answered 12/5, 2015 at 23:37 Comment(2)
my_pet doesn't need to be deleted as it is non a dynamically allocated object. Do you mean Pet* my_pet = new Pet(0, "Henry"); ?Jarid
You should probably be taking your const Pet and const std::string arguments as reference, here Pet(const Pet new_pet) and here Pet(const int new_id, const std::string new_name) for efficiency, there's not a need to copy data here.Expensive
J
10

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:

Pet* pet = NULL; // or nullptr

and later assign it an instance of Pet:

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

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

if (pet == NULL) // or nullptr
{
    ...
}
Jarid answered 12/5, 2015 at 23:45 Comment(3)
This is what I was looking for, thanks @Levi. How would I go about forcing the deletion of the pet... For example, Say a Pet was assigned to my_pet. But at some point I want to make my_pet not hold a pet. Would this work...? my_pet = NULL;Rosenstein
you can just call delete petJarid
@Rosenstein "should work great then. " No, i don't really think so. You shouldn't need to manage new/´delete` on your own. Use smart pointers, as mentioned with my answer instead.Farias
L
0

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

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;

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


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.

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

std::unique_ptr<Pet> my_pet(new Pet(0, "Henry"));`
Louth answered 12/5, 2015 at 23:44 Comment(4)
delete(my_pet) is perfectly validJarid
@Jarid Sure?? You did notice that my_pet isn't even a pointer variable, didn't you?Farias
well, if delete my_pet is valid, it is safe to assume that delete (my_pet) is tooJarid
oh wait my bad, I misread the answer to mean 'you should use delete my_pet instead of delete (my_pet)Jarid
O
0

Something like this?

template<typename T>
void CheckDependency(const T& dependency) {
    if(dependency == nullptr) {
        throw std::runtime_error("Not created" + std::string{typeid(dependency).name()});
    }
}
Oxcart answered 14/6, 2024 at 16:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.