I'm not sure if
我不确定
return *this
is the only way we could return an instance of a class who called a member function? The reason why I asked is because our instructor told us to avoid using pointers if necessary and I'm wondering if this is a case where the only necessary way to do it is by returning the this pointer.
是唯一可以返回调用成员函数的类实例的方法吗?我问的原因是因为我们的教练告诉我们如果有必要避免使用指针,我想知道这是否是唯一必要的方法是返回this指针。
I'm working with a fraction class that holds private data members numerator and denominator. The member function I'm talking about is used to add two fractions for example:
我正在使用一个包含私有数据成员分子和分母的分数类。我正在谈论的成员函数用于添加两个分数,例如:
Fraction C = A.plus(B);
plus member function is defined as this:
加上成员函数定义如下:
Fraction& plus( const Fraction frac )
The instructor wants us to do C = A += B , so I guess that's why.
教练要我们做C = A + = B,所以我猜这就是原因。
7 个解决方案
#1
Get a new instructor. It looks as if the declaration of plus() is completely wrong.
找一位新教练。看起来好像是()的声明是完全错误的。
- it probably should return a value rather than a reference
- if it must return a reference, it should return a const reference
- it should definitely take a const reference as a parameter
它可能应该返回一个值而不是一个引用
如果它必须返回一个引用,它应该返回一个const引用
它绝对应该将const引用作为参数
That is for likely sensible implementations of a member plus() function. Of course, it should probably be a friend.
这是成员plus()函数的合理实现。当然,它可能应该是朋友。
#2
I think in this case it is safe to use
我认为在这种情况下使用是安全的
return *this
because this
refers to the current object so it is guaranteed to exist, so it won't be null.
因为它引用当前对象所以它保证存在,所以它不会为null。
The reason plus
returns reference to itself is so that it can be chained:
plus返回引用自身的原因是它可以被链接:
Fraction C = A.plus(B).plus(D) // perhaps?
Note that in the above case C will be created by copying the result of addition. This also assumes that operation plus
is meant to modify object (in this case A) and return the reference to this modified object.
请注意,在上述情况下,将通过复制添加结果来创建C.这也假设操作加上意味着修改对象(在本例中为A)并返回对此修改对象的引用。
Wouldn't plus accept reference instead of making copy of the parameter?
不会加上接受引用而不是复制参数吗?
Fraction& plus( const Fraction& frac )
This is similar to how you would implement operator=
(an example):
这与您实现operator =(一个示例)的方式类似:
A& operator=(const A& right) {
if(this == &right) return *this; // Handle self-assignment
b = right.b;
return *this;
}
Maybe you would want to not modify object and return new object:
也许你不想修改对象并返回新对象:
// assuming there's a constructor Fraction(int numerator, int denominator):
Fraction* plus(Fraction const& rhs)
{
return new Fraction(numerator * rhs.denominator
+ rhs.numerator * denominator,
denominator * rhs.denominator);
}
But this of course has to return pointer to new instance which is not a reference as maybe required in your task (?).
但是,这当然必须返回指向新实例的指针,而该实例不是您的任务中可能需要的引用(?)。
Or even better:
甚至更好:
Fraction plus(Fraction const& rhs)
{
return Fraction(numerator * rhs.denominator
+ rhs.numerator * denominator,
denominator * rhs.denominator);
}
This will create Fraction in the space of calling function so there's no overhead of copying structure on return.
这将在调用函数空间中创建Fraction,因此在返回时没有复制结构的开销。
#3
Yes, this is the only way. The only way to access the current object in a method is via this, and it is a pointer.
是的,这是唯一的方法。访问方法中当前对象的唯一方法是通过它,它是一个指针。
It is fine, and is an accepted practice.
这很好,是一种公认的做法。
#4
There's nothing wrong with returning *this
. For example, that's how overloads of modifying operators are supposed to work. It seems like the plus method is really just a way of providing an operator+= for your class without actually overloading the operator (I assume you haven't gotten to operator overloading yet), so returning *this
in this case is the usual behavior.
返回*这没有什么不对。例如,这就是修改运算符的重载应该如何工作。似乎plus方法实际上只是为你的类提供运算符+ =而不实际重载运算符(我假设你还没有得到运算符重载),所以在这种情况下返回* this是通常的行为。
#5
In your plus()
method you should probably create a new Fraction
object and return that, instead of modifying the current one and then returning *this
. You probably don't want to change A
in A.plus(B)
. To return a new Fraction
, the signature of plus()
would best be:
在你的plus()方法中,你应该创建一个新的Fraction对象并返回它,而不是修改当前的对象然后返回* this。您可能不想在A.plus(B)中更改A.要返回一个新的Fraction,plus()的签名最好是:
Fraction plus(const Fraction &frac);
(In case you're not currently modifying this
in the plus()
method, why do you want to return *this
?)
(如果您当前没有在plus()方法中修改它,为什么要返回* this?)
#6
I believe that the semantics should be that the member function 'plus' returns a 'new' object which represents the sum of the caller and the called. note :: new does not mean 'new' keyword in C++ :) so for your example,
我认为语义应该是成员函数'plus'返回一个'new'对象,它表示调用者和被调用者的总和。 note :: new并不意味着C ++中的'new'关键字:)所以对于你的例子,
// the constructor accepts (numerator, denominator).
// in your case, the caller object would be A, and the called object would be B(other).
return Fraction(numerator * other.denominator + other.numerator * denominator, denominator * other.denominator);
The only place I see it correct to return a reference to this is when you overload operators that have side effects.
我认为返回对此引用的唯一正确位置是重载具有副作用的运算符。
To clarify more, this should be your 'plus' signature,
为了澄清更多,这应该是你的“加号”签名,
Fraction plus( const Fraction& frac );
neither the caller nor the called should be effected by the 'plus'.
呼叫者和呼叫者都不应受“加号”影响。
#7
our instructor told us to avoid using pointers if necessary
我们的教练告诉我们如有必要避免使用指针
You're actually returning the value of dereferencing a pointer, not the pointer. So you should be good.
您实际上是返回取消引用指针的值,而不是指针。所以你应该好。
Personally, I never explicitly call methods or refer to members via this
. That is, I DO NOT do the following:
就个人而言,我从未明确地通过此方式调用方法或引用成员。也就是说,我不做以下事情:
class A {
public:
int x;
int get_x()
{
return this->x;
}
int get_x_plus_5()
{
return this->get_x() + 5;
}
}
However, I am perfectly fine with returning *this
.
但是,我很高兴回来*这个。
Your instructor probably is trying to get you to avoid (1) returning pointers to objects on the stack from functions (which means that they won't exist after the function exits) and (2) allocating objects on the free store when you don't have to. this
doesn't suffer from either of those issues.
您的教师可能正试图让您避免(1)从函数返回指向堆栈上对象的指针(这意味着函数退出后它们将不存在)和(2)当您不在时,在免费商店上分配对象必须。这不会受到这两个问题的影响。
#1
Get a new instructor. It looks as if the declaration of plus() is completely wrong.
找一位新教练。看起来好像是()的声明是完全错误的。
- it probably should return a value rather than a reference
- if it must return a reference, it should return a const reference
- it should definitely take a const reference as a parameter
它可能应该返回一个值而不是一个引用
如果它必须返回一个引用,它应该返回一个const引用
它绝对应该将const引用作为参数
That is for likely sensible implementations of a member plus() function. Of course, it should probably be a friend.
这是成员plus()函数的合理实现。当然,它可能应该是朋友。
#2
I think in this case it is safe to use
我认为在这种情况下使用是安全的
return *this
because this
refers to the current object so it is guaranteed to exist, so it won't be null.
因为它引用当前对象所以它保证存在,所以它不会为null。
The reason plus
returns reference to itself is so that it can be chained:
plus返回引用自身的原因是它可以被链接:
Fraction C = A.plus(B).plus(D) // perhaps?
Note that in the above case C will be created by copying the result of addition. This also assumes that operation plus
is meant to modify object (in this case A) and return the reference to this modified object.
请注意,在上述情况下,将通过复制添加结果来创建C.这也假设操作加上意味着修改对象(在本例中为A)并返回对此修改对象的引用。
Wouldn't plus accept reference instead of making copy of the parameter?
不会加上接受引用而不是复制参数吗?
Fraction& plus( const Fraction& frac )
This is similar to how you would implement operator=
(an example):
这与您实现operator =(一个示例)的方式类似:
A& operator=(const A& right) {
if(this == &right) return *this; // Handle self-assignment
b = right.b;
return *this;
}
Maybe you would want to not modify object and return new object:
也许你不想修改对象并返回新对象:
// assuming there's a constructor Fraction(int numerator, int denominator):
Fraction* plus(Fraction const& rhs)
{
return new Fraction(numerator * rhs.denominator
+ rhs.numerator * denominator,
denominator * rhs.denominator);
}
But this of course has to return pointer to new instance which is not a reference as maybe required in your task (?).
但是,这当然必须返回指向新实例的指针,而该实例不是您的任务中可能需要的引用(?)。
Or even better:
甚至更好:
Fraction plus(Fraction const& rhs)
{
return Fraction(numerator * rhs.denominator
+ rhs.numerator * denominator,
denominator * rhs.denominator);
}
This will create Fraction in the space of calling function so there's no overhead of copying structure on return.
这将在调用函数空间中创建Fraction,因此在返回时没有复制结构的开销。
#3
Yes, this is the only way. The only way to access the current object in a method is via this, and it is a pointer.
是的,这是唯一的方法。访问方法中当前对象的唯一方法是通过它,它是一个指针。
It is fine, and is an accepted practice.
这很好,是一种公认的做法。
#4
There's nothing wrong with returning *this
. For example, that's how overloads of modifying operators are supposed to work. It seems like the plus method is really just a way of providing an operator+= for your class without actually overloading the operator (I assume you haven't gotten to operator overloading yet), so returning *this
in this case is the usual behavior.
返回*这没有什么不对。例如,这就是修改运算符的重载应该如何工作。似乎plus方法实际上只是为你的类提供运算符+ =而不实际重载运算符(我假设你还没有得到运算符重载),所以在这种情况下返回* this是通常的行为。
#5
In your plus()
method you should probably create a new Fraction
object and return that, instead of modifying the current one and then returning *this
. You probably don't want to change A
in A.plus(B)
. To return a new Fraction
, the signature of plus()
would best be:
在你的plus()方法中,你应该创建一个新的Fraction对象并返回它,而不是修改当前的对象然后返回* this。您可能不想在A.plus(B)中更改A.要返回一个新的Fraction,plus()的签名最好是:
Fraction plus(const Fraction &frac);
(In case you're not currently modifying this
in the plus()
method, why do you want to return *this
?)
(如果您当前没有在plus()方法中修改它,为什么要返回* this?)
#6
I believe that the semantics should be that the member function 'plus' returns a 'new' object which represents the sum of the caller and the called. note :: new does not mean 'new' keyword in C++ :) so for your example,
我认为语义应该是成员函数'plus'返回一个'new'对象,它表示调用者和被调用者的总和。 note :: new并不意味着C ++中的'new'关键字:)所以对于你的例子,
// the constructor accepts (numerator, denominator).
// in your case, the caller object would be A, and the called object would be B(other).
return Fraction(numerator * other.denominator + other.numerator * denominator, denominator * other.denominator);
The only place I see it correct to return a reference to this is when you overload operators that have side effects.
我认为返回对此引用的唯一正确位置是重载具有副作用的运算符。
To clarify more, this should be your 'plus' signature,
为了澄清更多,这应该是你的“加号”签名,
Fraction plus( const Fraction& frac );
neither the caller nor the called should be effected by the 'plus'.
呼叫者和呼叫者都不应受“加号”影响。
#7
our instructor told us to avoid using pointers if necessary
我们的教练告诉我们如有必要避免使用指针
You're actually returning the value of dereferencing a pointer, not the pointer. So you should be good.
您实际上是返回取消引用指针的值,而不是指针。所以你应该好。
Personally, I never explicitly call methods or refer to members via this
. That is, I DO NOT do the following:
就个人而言,我从未明确地通过此方式调用方法或引用成员。也就是说,我不做以下事情:
class A {
public:
int x;
int get_x()
{
return this->x;
}
int get_x_plus_5()
{
return this->get_x() + 5;
}
}
However, I am perfectly fine with returning *this
.
但是,我很高兴回来*这个。
Your instructor probably is trying to get you to avoid (1) returning pointers to objects on the stack from functions (which means that they won't exist after the function exits) and (2) allocating objects on the free store when you don't have to. this
doesn't suffer from either of those issues.
您的教师可能正试图让您避免(1)从函数返回指向堆栈上对象的指针(这意味着函数退出后它们将不存在)和(2)当您不在时,在免费商店上分配对象必须。这不会受到这两个问题的影响。