C++;类别:创建“的副本”;这";成员函数中的类 我试图为一个C++类的 += < /代码>运算符编写一个函数,它使用了已经编写的>+/Cuth>操作符函数。到目前为止,我没有成功地将这个指针与+操作符关联起来。这些是我在g++中编译的一些尝试,但没有产生预期的结果。我曾两次试图简单地复制这个类,但似乎没有成功 intstr&intstr::operator+=(常量intstr&i) { intstr*a; a=新的intstr; *a=*此+i; 返回*a; } intstr&intstr::operator+=(常量intstr&i) { intstr*a,b(*本); a=新的intstr; *a=b+i; 返回*a; } intstr&intstr::operator+=(常量intstr&i) { intstr*a,*b; a=新的intstr; b=这个; *a=*b+i; 返回*a; } intstr&intstr::operator+=(常量intstr&i) { intstr*a; a=新的intstr; *a=此->运算符+(i); 返回*a; }
在测试代码中,我所做的只是将代码C++;类别:创建“的副本”;这";成员函数中的类 我试图为一个C++类的 += < /代码>运算符编写一个函数,它使用了已经编写的>+/Cuth>操作符函数。到目前为止,我没有成功地将这个指针与+操作符关联起来。这些是我在g++中编译的一些尝试,但没有产生预期的结果。我曾两次试图简单地复制这个类,但似乎没有成功 intstr&intstr::operator+=(常量intstr&i) { intstr*a; a=新的intstr; *a=*此+i; 返回*a; } intstr&intstr::operator+=(常量intstr&i) { intstr*a,b(*本); a=新的intstr; *a=b+i; 返回*a; } intstr&intstr::operator+=(常量intstr&i) { intstr*a,*b; a=新的intstr; b=这个; *a=*b+i; 返回*a; } intstr&intstr::operator+=(常量intstr&i) { intstr*a; a=新的intstr; *a=此->运算符+(i); 返回*a; },c++,class,operator-keyword,C++,Class,Operator Keyword,在测试代码中,我所做的只是将代码a=a+I的工作行替换为a+=I,因此我怀疑问题是否存在,但这是可能的。执行此操作的唯一方法是将代码从+操作符复制到+=函数中吗?通常方法是相反的:实现操作符+=,然后使用该实现实现操作符+(创建第一个参数的副本,然后使用+=增加第二个参数并返回该参数) 除此之外,为什么在所有版本中都要调用new?对于操作符+=,您根本不需要创建任何新对象。操作符应该通过将左侧操作数的值与右侧操作数的值相加来修改左侧操作数的值。无需在任何地方创建新对象(与new!)的动态分配不太
a=a+I
的工作行替换为a+=I
,因此我怀疑问题是否存在,但这是可能的。执行此操作的唯一方法是将代码从+
操作符复制到+=
函数中吗?通常方法是相反的:实现操作符+=
,然后使用该实现实现操作符+
(创建第一个参数的副本,然后使用+=
增加第二个参数并返回该参数)
除此之外,为什么在所有版本中都要调用
new
?对于操作符+=
,您根本不需要创建任何新对象。操作符应该通过将左侧操作数的值与右侧操作数的值相加来修改左侧操作数的值。无需在任何地方创建新对象(与new
!)的动态分配不太一样。通常方法是相反的:实现操作符+=
,然后使用该实现实现操作符+
(创建第一个参数的副本,然后使用+=
增加第二个参数并返回该参数)
除此之外,为什么在所有版本中都要调用
new
?对于操作符+=
,您根本不需要创建任何新对象。操作符应该通过将左侧操作数的值与右侧操作数的值相加来修改左侧操作数的值。无需在任何地方创建新对象(与new
!)的动态分配不太一样。通常方法是相反的:实现操作符+=
,然后使用该实现实现操作符+
(创建第一个参数的副本,然后使用+=
增加第二个参数并返回该参数)
除此之外,为什么在所有版本中都要调用
new
?对于操作符+=
,您根本不需要创建任何新对象。操作符应该通过将左侧操作数的值与右侧操作数的值相加来修改左侧操作数的值。无需在任何地方创建新对象(与new
!)的动态分配不太一样。通常方法是相反的:实现操作符+=
,然后使用该实现实现操作符+
(创建第一个参数的副本,然后使用+=
增加第二个参数并返回该参数)
除此之外,为什么在所有版本中都要调用
new
?对于操作符+=
,您根本不需要创建任何新对象。操作符应该通过将左侧操作数的值与右侧操作数的值相加来修改左侧操作数的值。无需在任何地方创建新对象(与new
!)一起动态分配的情况更少)操作员可能看起来像
intstr& intstr::operator+=( const intstr& i )
{
*this = *this + i;
return *this;
}
如果操作符+
被声明为类成员函数,那么您也可以编写
intstr& intstr::operator+=( const intstr& i )
{
*this = operator +( i ); // or *this = this->operator +( i );
return *this;
}
在运算符内动态分配intstr类型的对象是错误的。至少不需要这样做。运算符可能看起来像
intstr& intstr::operator+=( const intstr& i )
{
*this = *this + i;
return *this;
}
如果操作符+
被声明为类成员函数,那么您也可以编写
intstr& intstr::operator+=( const intstr& i )
{
*this = operator +( i ); // or *this = this->operator +( i );
return *this;
}
在运算符内动态分配intstr类型的对象是错误的。至少不需要这样做。运算符可能看起来像
intstr& intstr::operator+=( const intstr& i )
{
*this = *this + i;
return *this;
}
如果操作符+
被声明为类成员函数,那么您也可以编写
intstr& intstr::operator+=( const intstr& i )
{
*this = operator +( i ); // or *this = this->operator +( i );
return *this;
}
在运算符内动态分配intstr类型的对象是错误的。至少不需要这样做。运算符可能看起来像
intstr& intstr::operator+=( const intstr& i )
{
*this = *this + i;
return *this;
}
如果操作符+
被声明为类成员函数,那么您也可以编写
intstr& intstr::operator+=( const intstr& i )
{
*this = operator +( i ); // or *this = this->operator +( i );
return *this;
}
在运算符中动态分配intstr类型的对象是错误的。至少不需要这样做。通常情况下,您可以采用另一种方式:
intstr& intstr::operator+=(intstr const& rhs)
{
// Do this stuff to add rhs into this object.
return *this;
}
// Then + is implemented in terms of +=
intstr intstr::operator+(intstr const& rhs)
{
instr result(*this); // Make a copy as the result
result += rhs;
return result;
}
// Then your free functions as needed.
通常情况下,您会采用另一种方式:
intstr& intstr::operator+=(intstr const& rhs)
{
// Do this stuff to add rhs into this object.
return *this;
}
// Then + is implemented in terms of +=
intstr intstr::operator+(intstr const& rhs)
{
instr result(*this); // Make a copy as the result
result += rhs;
return result;
}
// Then your free functions as needed.
通常情况下,您会采用另一种方式:
intstr& intstr::operator+=(intstr const& rhs)
{
// Do this stuff to add rhs into this object.
return *this;
}
// Then + is implemented in terms of +=
intstr intstr::operator+(intstr const& rhs)
{
instr result(*this); // Make a copy as the result
result += rhs;
return result;
}
// Then your free functions as needed.
通常情况下,您会采用另一种方式:
intstr& intstr::operator+=(intstr const& rhs)
{
// Do this stuff to add rhs into this object.
return *this;
}
// Then + is implemented in terms of +=
intstr intstr::operator+(intstr const& rhs)
{
instr result(*this); // Make a copy as the result
result += rhs;
return result;
}
// Then your free functions as needed.
感谢您的快速回复!起初,我认为我不需要“new”,但直到我使用它,我的代码才得以编译,所以我才使用它。我从未想过先使用“+=”并从中派生“+”—很高兴知道!我会试试。感谢您的快速回复!起初,我认为我不需要“new”,但直到我使用它,我的代码才编译,所以我才使用它。我从来没有想过先使用“+=”并从中派生“+”-很高兴知道!我会试试。谢谢你的快速回复!起初,我认为我不需要“new”,但直到我使用它,我的代码才编译,所以我才使用它。我从来没有想过使用“+=”首先,从中派生出“+”——很高兴知道!我试试t