Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

};
like image 964
pharma_joe Avatar asked Sep 04 '10 03:09

pharma_joe


2 Answers

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!

like image 68
Doug T. Avatar answered Oct 24 '22 04:10

Doug T.


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

ie.

SomeClass unloadingShip

versus

SomeClass *unloadingShip

like image 1
GWW Avatar answered Oct 24 '22 02:10

GWW