简体   繁体   English

赋值运算符c ++ NULL赋值

[英]assignment operator c++ NULL assignment

I am working with a class that is mostly working fine, but I am doing some functionality that I might be having a recursive function return a NULL pointer of the class type as a control, and so it might be assigning a NULL pointer to my class object so long story short: 我正在使用一个大多数情况下都可以正常工作的类,但是我正在做一些功能,可能有一个递归函数将类类型的NULL指针作为控件返回,因此它可能为我的类分配了NULL指针。长话短说的对象:

Thing& Thing::operator=(Thing * _other){
    if (_other == NULL){
        *this = NULL;        // compiler throws here
        return *this;    
    }
    // does other assignment work
    return *this;
}

my compiler VS2010 throws that this is not an I-value. 我的编译器VS2010认为this不是I值。 so how do I set the value to NULL , or is it even possible to set an item to NULL from inside? 那么如何将值设置为NULL ,或者甚至有可能从内部将项目设置为NULL?

EDIT: modified this to *this though for some reason now the program breaks with infinite calls to the assignment operator. 编辑: this修改为*this尽管由于某种原因现在程序因无限调用赋值运算符而中断。 no idea what is going on 不知道发生了什么

You cannot directly set a value to the this pointer. 您不能直接为this指针设置值。 Hence 因此

this = NULL;

is semantically and syntactically wrong. 在语义和语法上都是错误的。

You can use exceptions to check if _other is NULL . 您可以使用异常来检查_other是否为NULL For example: 例如:

class null_object_exception : public virtual exception {};
...
if (_other == NULL) throw null_object_exception();

To perform a NULL assignment: 要执行NULL分配:

Thing the_thing, other_thing;
try { the_thing = &other_thing; }
catch( const null_object_exception& e ) { the_thing = NULL; }

You are trying to write a "nullable" class. 您正在尝试编写“可空”类。 Consider the "null" state to be one of the states an instance of Thing can be in, and don't convolute it with pointer semantics. 将“空”状态视为Thing实例可以处于的状态之一,并且不要用指针语义对其进行卷积。

The general method would be to add a boolean flag to your class that keeps track of whether an instance is in null state or not. 通用方法是在您的类中添加一个布尔标志,以跟踪实例是否处于空状态。 I would implement it like this: 我会这样实现:

class Thing
{
private:
    bool m_null;

public:
    void setnull() { m_null = true; }
    bool isnull() const { return m_null; }

    Thing() : m_null(true) {}

    ... // rest of class
};

And now the default assignment operator works fine. 现在,默认赋值运算符可以正常工作。

You CANNOT assign to this . 不能分配到this

Also, you should take your arguments by const reference, so Thing& operator= (const Thing& other) 另外,您应该通过const引用获取参数,因此Thing& operator= (const Thing& other)

There's a great SO question in C++-faq tag about operator overloading, you can find it here C ++-faq标记中有一个关于运算符重载的很棒的SO问题,您可以在这里找到

what are the member variables of Thing class? Thing类的成员变量是什么? if you want to show that the object is somehow without value or not intialized, you would better assign fileds 0 (for integers) and null(for pointers) etc, instead of assigning "this" which is constant. 如果要显示该对象某种程度上没有值或没有初始化,则最好将字段分配为0(对于整数)和null(对于指针)等,而不是分配恒定的“ this”。

Short answer, no, you can't assign to this in C++. 简短的答案,不,您不能在C ++中分配给this

Longer answer; 更长的答案; for your assignment operator to even be called, you'd have to have a construct like; 为了让您的赋值运算符被调用,您必须具有类似的构造;

MyObject a;
a = NULL;    // can change the content of `a` but not which object the name `a` refers to.

If you're thinking about this construct; 如果您正在考虑这种结构;

MyObject *a;
a = NULL;

your assignment operator won't even be called since it is an operator on the object , not the pointer . 您的赋值运算符甚至不会被调用,因为它是对象上的运算符,而不是指针 Assigning to the pointer a will work without you defining an assignment operator. 无需定义分配运算符,即可将指针分配给指针a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM