2010-09-04 74 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; 
} 

因此,通過這種方式,您可以在刪除對象之前或在創建新對象之前檢查對象是否存在。

希望這會有所幫助!