2010-09-04 71 views
13

我想写一个对象是否存在,将检查功能:我怎么能检查对象是否存在于C++

bool UnloadingBay::isEmpty() { 
    bool isEmpty = true; 
    if(this->unloadingShip != NULL) { 
     isEmpty = false; 
    } 
    return isEmpty; 
} 

我是相当新的C++和不知道如果我的Java背景是混乱东西,但编译器给出了一个错误:

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

我似乎无法弄清楚为什么它不起作用。

下面是类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(); 

}; 
+0

正在卸载船舶类功能?你能发布你的类声明吗? – 2010-09-04 03:14:52

+0

no unloadingShip是unLoadingBay的一个属性,它是isEmpty是其成员的类 – 2010-09-04 03:20:16

+4

pharma_joe:作为一般提示:您在Java中学到的所有内容?算了吧。它不会帮助你在C++中。 – greyfade 2010-09-04 04:45:48

回答

22

这听起来像你可能需要在C++中的“变量”的概念的初级读物。

在C++中,每个变量的生命周期都与其范围有关。的最简单的例子是一个函数的局部变量:

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 

在上面的代码“anUnloadingShip”被输入了函数foo时构造缺省(即进入其范围)。不需要“新”。当包含范围消失时(在这种情况下,当foo退出时),将自动调用用户定义的析构函数来清理UnloadingShip。关联的内存会自动清理。

当涵盖范围是一个C++类(即一个成员变量):

class UnloadingBay 
{ 
    int foo; 
    UnloadingShip unloadingShip; 
}; 

寿命是联系在一起的类的实例,因此,当我们的函数创建了一个“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*/ 

只要“aBay”生活,aBay的成员就构建并生活。

这一切都在编译时间。没有运行时参考计数防止破坏。没有考虑任何其他可能refer topoint to那个变量。编译器分析我们编写的函数来确定变量的范围,从而确定变量的生命周期。编译器会查看变量范围的结束位置,清理该变量所需的任何内容都将在编译时插入。

“新”,“NULL”,(不要忘记“删除”)在C + +发挥指针。指针是一种保存某个对象的内存地址的变量。程序员使用值“NULL”来表示一个指针不包含地址(即它不指向任何东西)。如果你不使用指针,你不需要考虑NULL。

直到你掌握了C++中的变量如何进出范围,避免指针。这完全是另一个话题。

祝你好运!

+0

非常感谢,我非常感谢。指针给我一些理解的麻烦,但我到了那里。干杯 – 2010-09-04 04:28:49

+0

我喜欢“保证”的部分:-)特别是如果一些家伙决定在析构函数中抛出异常! – 2010-09-07 21:37:21

+0

@Vlad,当你抛出一个析构函数时,你的程序保证会消失,所以变量消失:) – 2010-09-08 02:09:07

1

我假设unloadingShip是一个对象,而不是一个指针,以便值不可能是NULL。

即。

SomeClass的unloadingShip

SomeClass的* unloadingShip

+0

是的,它是一个对象。我如何检查卸载船是否被实例化? – 2010-09-04 03:32:56

+3

如果UnloadingBay被实例化,那么它的unloadingShip对象也被实例化(即使你在UnloadingBay构造函数中)。 – EboMike 2010-09-04 03:35:55

+0

如果“this”(又名UnloadingBay对象)被实例化,那么unloadingShip也会自动实例化,因为unloadingShip是UnloadingBay对象的一部分。 即,如果您愿意,UnloadingBay对象和UnloadingShip对象都是同一块内存的一部分。它不像每个单独的对象都必须单独分配的Java。 – 2010-09-04 03:38:14

1

好了,你不用写了这么多的代码来检查,如果指针为空或不是。该方法可简单得多:

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

此外,它应该被标记为“常量”,因为它不修改对象的状态,可以在恒定的情况下,被称为好。

在你的情况下,“unloadingShip”是类“UnloadingShip”的一个对象,它不是动态分配的(除非动态分配整个类“UnloadingBay”)。因此,检查它是否等于NULL没有意义,因为它不是指针。

+0

好的,我已经添加了声明。谢谢! – 2010-09-04 03:41:31

1

进行检查,如果一个对象存在,你可以考虑去这样说:

创建一个指向你的对象:

someClass *myObj = NULL // Make it null 

,现在,你通过这个指针,你可以检查:

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

,然后如果你想删除,你可以这样做:

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

因此,通过这种方式,您可以在删除对象之前或在创建新对象之前检查对象是否存在。

希望这会有所帮助!