如何检查C ++中是否存在对象 [英] how can I check if an object exists in C++

查看:57
本文介绍了如何检查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;
}

我对C ++还是很陌生,不确定我的Java背景是否令人困惑,但是编译器给出了一个错误:

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.

这是UnloadingBay类的声明:

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

};

推荐答案

听起来您可能需要C ++中变量"概念的入门知识.

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

在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

在上面的代码中,当输入函数foo(即,输入了它的作用域)时,默认构造了"anUnloadingShip".无需新".当包含范围消失时(在这种情况下,当foo退出时),将自动调用用户定义的析构函数以清理UnloadingShip.关联的内存会自动清除.

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.

当包含范围是C ++类(即成员变量)时:

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

class UnloadingBay
{
   int foo;
   UnloadingShip unloadingShip;
};

生存期与类的实例相关,因此当我们的函数创建"UnloadingBay"时

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*/

只要"aBay"存在,aBay的成员就可以构建并生存.

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.

C ++中的"new","NULL"(不要忘记"delete")与指针一起起作用.指针是一种变量,用于保存某些对象的内存地址.程序员使用值"NULL"表示指针不保存地址(即,它不指向任何内容).如果您不使用指针,则无需考虑NULL.

"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.

在您掌握了C ++中变量如何进入和超出作用域之前,请避免使用指针.完全是另一个话题.

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

祝你好运!

这篇关于如何检查C ++中是否存在对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆