开发者

how can I check if an object exists in C++

I am trying to write a function that will check if an object exists:

bool UnloadingBay::isEmpty() {
    bool isEmpty = true开发者_运维百科;
    if(this->unloadingShip != NULL) {
        isEmpty = false;
    }
    return isEmpty;
}

I am pretty new to C++ and not sure if my Java background is confusing something, but the compiler gives an error:

UnloadingBay.cpp:36: error: no match for ‘operator!=’ in ‘((UnloadingBay*)this)->UnloadingBay::unloadingShip != 0’

I can't seem to figure out why it doesn't work.

Here is the declaration for class UnloadingBay:

class UnloadingBay {

    private:
        Ship unloadingShip;

    public:
        UnloadingBay();
        ~UnloadingBay();

        void unloadContainer(Container container);
        void loadContainer(Container container);
        void dockShip(Ship ship);
        void undockShip(Ship ship);
        bool isEmpty();

};


It sounds like you may need a primer on the concept of a "variable" in C++.

In C++ every variable's lifetime is tied to it's encompassing scope. The simplest example of this is a function's local variables:

void foo() // foo scope begins
{  
    UnloadingShip anUnloadingShip; // constructed with default constructor

    // do stuff without fear!
    anUnloadingShip.Unload();
} // // foo scope ends, anything associated with it guaranteed to go away

In the above code "anUnloadingShip" is default constructed when the function foo is entered (ie its scope is entered). No "new" required. When the encompassing scope goes away (in this case when foo exits), your user-defined destructor is automatically called to clean up the UnloadingShip. The associated memory is automatically cleaned up.

When the encompassing scope is a C++ class (that is to say a member variable):

class UnloadingBay
{
   int foo;
   UnloadingShip unloadingShip;
};

the lifetime is tied to the instances of the class, so when our function creates an "UnloadingBay"

void bar2()
{
    UnloadingBay aBay; /*no new required, default constructor called,
                         which calls UnloadingShip's constructor for
                         it's member unloadingShip*/

    // do stuff!
}  /*destructor fires, which in turn trigger's member's destructors*/

the members of aBay are constructed and live as long as "aBay" lives.

This is all figured out at compile time. There is no run-time reference counting preventing destruction. No considerations are made for anything else that might refer to or point to that variable. The compiler analyzes the functions we wrote to determine the scope, and therefore lifetime, of the variables. The compiler sees where a variable's scope ends and anything needed to clean up that variable will get inserted at compile time.

"new", "NULL", (don't forget "delete") in C++ come into play with pointers. Pointers are a type of variable that holds a memory address of some object. Programmers use the value "NULL" to indicate that a pointer doesn't hold an address (ie it doesn't point to anything). If you aren't using pointers, you don't need to think about NULL.

Until you've mastered how variables in C++ go in and out of scope, avoid pointers. It's another topic entirely.

Good luck!


I'm assuming unloadingShip is an object and not a pointer so the value could never be NULL.

ie.

SomeClass unloadingShip

versus

SomeClass *unloadingShip


Well, you don't have to write so much code to check if a pointer is NULL or not. The method could be a lot simpler:

bool UnloadingBay::isEmpty() const {
    return unloadingShip == NULL;
}

Plus, it should be marked as "const" because it does not modify the state of the object and can be called on constant instances as well.

In your case, "unloadingShip" is an object of class "UnloadingShip" which is not dynamically allocated (except when the whole class "UnloadingBay" is allocated dynamically). Thus, checking if it equals to NULL doesn't make sense because it is not a pointer.


For checking, if an object exists, you can consider going this way:

create a pointer to your object:

someClass *myObj = NULL // Make it null

and now where you pass this pointer, you can check:

if(!myObj)  // if its set null, it wont pass this condition
    myObj = new someClass();

and then in case you want to delete, you can do this:

if(myobj)
{
    delete myObj;
    myObj = NULL;
}

so in this way, you can have a good control on checking whether your object exists, before deleting it or before creating a new one.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜