简体   繁体   English

可以重载赋值运算符以返回 class 的属性值吗?

[英]Can assignment operator be overloaded to return the value of a property of a class?

I wanted to return the value of a property of a class using assignment operator.我想使用赋值运算符返回 class 的属性值。 I tried to fulfill this purpose.我试图实现这个目的。 I searched a lot on the web but all of the websites I visited talked about how to overload assignment operator to do like a copy constructor of a class like this: class_t& operator=(class_t&);我在 web 上进行了很多搜索,但我访问的所有网站都谈到了如何重载赋值运算符,就像 class 的复制构造函数一样: class_t& operator=(class_t&); . . Can anybody help me overload this operator to return the value of a property of a class?谁能帮我重载此运算符以返回 class 的属性值?

This is my code:这是我的代码:

class A_t
{
private:
  int value = 0;

public:
  int operator = (A_t);  // I failed to overload assignment operator for this
  A_t& operator = (int); // I succeeded to overload assignment operator for this
  int Value();
  void setValue(int);
};

A_t& A_t::operator = (int value)
{
  this->setValue(value);
  return *this;
}

int operator = (A_t &data)
{
  return data.value;
}

int A_t::Value() { return this->value; }
void A_t::setValue(int data) { this->value = data; }

int main()
{
    A_t object = 3;
    int value = object; // Error: cannot convert 'A_t' to 'int' in initialization

    cout << value << endl;
    return 0;
}

For this, you need an int operator for your class which returns the variable when being assigned to an integer.为此,您的 class 需要一个int运算符,它在分配给 integer 时返回变量。 Plus the class missed the constructor required for A_t object = 3;加上 class 错过了A_t object = 3; . . The corrected class looks like this,修正后的 class 看起来像这样,

class A_t
{
private:
    int value = 0;

public:
    //int operator = (A_t);  <-- You dont need this.
    A_t& operator = (int); // I succeeded to overload assignment operator for this
    int Value();
    void setValue(int);

    /**
     * Construct using an integer value.
     * 
     * @param val: The value to be set.
     */
    A_t(int val) : value(val) {}

    /**
     * int operator.
     * 
     * @return The value stored inside.
     */
    operator int() const
    {
        return value;
    }

    /**
     * int& operator (optional).
     *
     * @return The variable stored inside.
     */
    operator int& ()
    {
        return value;
    }
};

A_t& A_t::operator = (int value)
{
    this->setValue(value);
    return *this;
}

int A_t::Value() { return this->value; }
void A_t::setValue(int data) { this->value = data; }

int main()
{
    A_t object = 3;
    int value = object; // Error: cannot convert 'A_t' to 'int' in initialization

    cout << value << endl;
    return 0;
}

You can't overload operator = for this.不能为此重载operator = What you can do instead is overload the implicit conversion-to- int operator inside the class:您可以做的是重载 class 中的隐式转换为int运算符:

operator int() const { return value; }

However, think carefully whether this is actually a good idea in your case.但是,请仔细考虑在您的情况下这是否真的是一个好主意。 Implicit conversions should usually be avoided at all cost, because it's very error-prone (many smart people think C++ shouldn't allow defining custom implicit conversions at all.).通常应该不惜一切代价避免隐式转换,因为它非常容易出错(许多聪明的人认为 C++ 根本不应该允许定义自定义隐式转换。)。

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

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