重载 = 在 C++ 中

发布于 2024-07-16 13:36:45 字数 1495 浏览 3 评论 0原文

我正在尝试重载赋值运算符,并且希望清除一些内容(如果可以的话)。

我有一个非成员函数, bool operator==( const MyClass& obj1, const myClass& obj2 ) 定义了我的类的 oustide 。

由于显而易见的原因,我无法联系到我的任何私人会员。

所以我认为我需要做的是重载赋值运算符。 并在非成员函数中进行赋值。

话虽如此,我认为我需要执行以下操作:

  1. 使用我的函数并使用 strcpystrdup 复制信息。 我使用了strcpy。
  2. 转到赋值运算符, bool MyClass::operator=( const MyClass& obj1 );
  3. 现在我们进入函数重载(==)并将 obj2 分配给 obj1。

我没有复制构造函数,所以我被这些困住了:

class Class
{
private:
m_1;
m_2;
public:
..
};

void Class::Func1(char buff[]) const
{   
    strcpy( buff, m_1 );
    return;
}
void Class::Func2(char buff[]) const
{
    strcpy( buff, m_2 );
    return;
}

bool Class& Class::operator=(const Class& obj)
{ 
    if ( this != &obj ) // check for self assignment.
    {
        strcpy( m_1, obj.m_1 ); 
        // do this for all other private members.
    }
    return *this;
}

bool operator== (const Class& obj1, const Class& obj2)
{
         Class MyClass1, MyClass2;
    MyClass1 = obj1;
    MyClass2 = obj2;

         MyClass2 = MyClass1;
         // did this change anything?
// Microsofts debugger can not get this far.
    return true;
}

所以你可能会说,我完全迷失在这个重载中。 有小费吗? 我确实有一个完整的版本,仅使用 :: 重载相同的运算符,因此我的私有成员不会失去范围。 我将我的分配返回为 true,并且它在 main 中工作。 这是我书中的例子。

重载赋值运算符然后在 operator== 非成员函数中执行转换是否有效? 完成该步骤后,我是否能够在 main 中将对象相互分配?

I'm trying to overload the assignment operator and would like to clear a few things up if that's ok.

I have a non member function, bool operator==( const MyClass& obj1, const myClass& obj2 ) defined oustide of my class.

I can't get at any of my private members for obvious reasons.

So what I think I need to do is to overload the assignment operator. And make assignments in the non member function.

With that said, I think I need to do the following:

  1. use my functions and copy information using strcpy or strdup. I used strcpy.
  2. go to the assignment operator, bool MyClass::operator=( const MyClass& obj1 );
  3. Now we go to the function overloading (==) and assign obj2 to obj1.

I don't have a copy constructor, so I'm stuck with these:

class Class
{
private:
m_1;
m_2;
public:
..
};

void Class::Func1(char buff[]) const
{   
    strcpy( buff, m_1 );
    return;
}
void Class::Func2(char buff[]) const
{
    strcpy( buff, m_2 );
    return;
}

bool Class& Class::operator=(const Class& obj)
{ 
    if ( this != &obj ) // check for self assignment.
    {
        strcpy( m_1, obj.m_1 ); 
        // do this for all other private members.
    }
    return *this;
}

bool operator== (const Class& obj1, const Class& obj2)
{
         Class MyClass1, MyClass2;
    MyClass1 = obj1;
    MyClass2 = obj2;

         MyClass2 = MyClass1;
         // did this change anything?
// Microsofts debugger can not get this far.
    return true;
}

So as you can probably tell, I'm completely lost in this overloading. Any tips? I do have a completed version overloading the same operator, only with ::, so my private members won't lose scope. I return my assignments as true and it works in main. Which is the example that I have in my book.

Will overloading the assignment operator and then preforming conversions in the operator== non member function work? Will I then be able to assign objects to each other in main after having completed that step?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末 2024-07-23 13:36:45

您在这里有一些明显的错误,并且对于您实际想要实现的目标存在一些困惑。 首先,赋值运算符operator = 旨在将值从一个实例复制到另一个实例。 赋值运算符的返回值几乎总是对复制目标的非常量引用,以便您可以链接赋值:

Class & operator=(const Class &rhs)
{
  // copy the members

  return *this;
}

比较运算符 operator == 旨在执行两个实例的比较。 如果它们相等,它会返回布尔值 true:

boolean operator==(const Class &rhs) const
{
  // check if they are equal
  return something;
}

令人困惑的是,为什么要尝试复制值,或者分配给比较运算符中的实例?

You have a couple of obvious mistakes here and there is some confusion about what you are actually trying to achieve. Firstly, the assignment operator operator = is meant to copy the value from one instance to another. The return value of the assignment operator is almost always a non constant reference to the target of the copy, so that you can chain assignments:

Class & operator=(const Class &rhs)
{
  // copy the members

  return *this;
}

The comparison operator operator == is meant to perform a comparison of two instances. It returns a boolean true if they are equal:

boolean operator==(const Class &rhs) const
{
  // check if they are equal
  return something;
}

The confusion is why are you trying to copy values around, or maybe assign to the instances in the comparison operator?

喜爱皱眉﹌ 2024-07-23 13:36:45

Op== 不是赋值运算符。 夯; Op=(const T&) 是。

bool operator==(const T&lhs, const T&rhs) 是比较两个 T 的操作。 如果 lhs 等于 rhs,则无论您要编码的“等于”的定义是什么,它都会返回 true。

Op== isn't the assignment operator. T& Op= (const T&) is.

bool operator==(const T& lhs, const T& rhs) is the operation to compare two Ts. It returns true if lhs is equal to rhs, for whatever definition of "equal" you want to code.

jJeQQOZ5 2024-07-23 13:36:45

我猜你想比较这两个对象。 在这种情况下,您可以在“Class”类中重载运算符 == 。 您不需要赋值运算符。

    class Class
    {
    public:
        Class(int i) : m_i(i){}

         bool operator==( const Class& rhs)
         {
            return m_i == rhs.m_i;
         }
    private:
        int m_i;

    };


int main()
{
   Class t1(10), t2(10);
   bool b = (t1 == t2);
}

I am guessing that you want to compare the two objects. In that case, you can just overload the operator == in class "Class". You don't need assignment operator.

    class Class
    {
    public:
        Class(int i) : m_i(i){}

         bool operator==( const Class& rhs)
         {
            return m_i == rhs.m_i;
         }
    private:
        int m_i;

    };


int main()
{
   Class t1(10), t2(10);
   bool b = (t1 == t2);
}
彻夜缠绵 2024-07-23 13:36:45

我不确定我是否正确理解了这个问题。 但是,如果您尝试使用非成员函数检查相等性,并且仅因为无法访问类的私有成员而无法执行此操作,那么您可以将非成员函数声明为友元函数并使用它像这样:

class Test
{
public:
    Test(int i) : m_i(i){}
private:
    int m_i;

    friend bool operator==(Test&  first, Test& second);
};

bool operator==(Test&  first, Test& second)
{
    return first.m_i == second.m_i;
}

int main()
{
   Test t1(10), t2(10);
   bool b = (t1 == t2);
}

I am not sure whether I understood the question correctly. But if you trying to check the equality using a non-member function and can't do this only because you can't access the private members of the class, then you can declare the non-member function as a friend function and use it like this:

class Test
{
public:
    Test(int i) : m_i(i){}
private:
    int m_i;

    friend bool operator==(Test&  first, Test& second);
};

bool operator==(Test&  first, Test& second)
{
    return first.m_i == second.m_i;
}

int main()
{
   Test t1(10), t2(10);
   bool b = (t1 == t2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文