重载 = 在 C++ 中
我正在尝试重载赋值运算符,并且希望清除一些内容(如果可以的话)。
我有一个非成员函数, bool operator==( const MyClass& obj1, const myClass& obj2 )
定义了我的类的 oustide 。
由于显而易见的原因,我无法联系到我的任何私人会员。
所以我认为我需要做的是重载赋值运算符。 并在非成员函数中进行赋值。
话虽如此,我认为我需要执行以下操作:
- 使用我的函数并使用
strcpy
或strdup
复制信息。 我使用了strcpy。 - 转到赋值运算符, bool MyClass::operator=( const MyClass& obj1 );
- 现在我们进入函数重载(==)并将 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:
- use my functions and copy information using
strcpy
orstrdup
. I usedstrcpy
. - go to the assignment operator, bool MyClass::operator=( const MyClass& obj1 );
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在这里有一些明显的错误,并且对于您实际想要实现的目标存在一些困惑。 首先,赋值运算符operator = 旨在将值从一个实例复制到另一个实例。 赋值运算符的返回值几乎总是对复制目标的非常量引用,以便您可以链接赋值:
比较运算符
operator ==
旨在执行两个实例的比较。 如果它们相等,它会返回布尔值 true:令人困惑的是,为什么要尝试复制值,或者分配给比较运算符中的实例?
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:The comparison operator
operator ==
is meant to perform a comparison of two instances. It returns a boolean true if they are equal:The confusion is why are you trying to copy values around, or maybe assign to the instances in the comparison operator?
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.
我猜你想比较这两个对象。 在这种情况下,您可以在“Class”类中重载运算符 == 。 您不需要赋值运算符。
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.
我不确定我是否正确理解了这个问题。 但是,如果您尝试使用非成员函数检查相等性,并且仅因为无法访问类的私有成员而无法执行此操作,那么您可以将非成员函数声明为友元函数并使用它像这样:
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: