在 C++ 中返回“this”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6919330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return "this" in C++?
提问by nobody
In Java you can simply return this
to get the current object. How do you do this in C++?
在 Java 中,您可以简单return this
地获取当前对象。你如何在 C++ 中做到这一点?
Java:
爪哇:
class MyClass {
MyClass example() {
return this;
}
}
回答by Rob?
Well, first off, you can't return anything from a void
-returning function.
好吧,首先,您不能从void
-returning 函数返回任何内容。
There are three ways to return something which provides access to the current object: by pointer, by reference, and by value.
有三种方法可以返回提供对当前对象的访问的东西:通过指针、通过引用和通过值。
class myclass {
public:
// Return by pointer needs const and non-const versions
myclass* ReturnPointerToCurrentObject() { return this; }
const myclass* ReturnPointerToCurrentObject() const { return this; }
// Return by reference needs const and non-const versions
myclass& ReturnReferenceToCurrentObject() { return *this; }
const myclass& ReturnReferenceToCurrentObject() const { return *this; }
// Return by value only needs one version.
myclass ReturnCopyOfCurrentObject() const { return *this; }
};
As indicated, each of the three ways returns the current object in slightly different form. Which one you use depends upon which form you need.
如上所述,三种方式中的每一种都以稍微不同的形式返回当前对象。您使用哪一种取决于您需要哪种形式。
回答by lucky85dog
One of the main advantages of return by referencein classes is the ability to easily chain functions.
在类中通过引用返回的主要优点之一是能够轻松地链接函数。
Suppose that your member function is to multiply a particular member of your class.
If you make the header and source files to keep the information of the class and the definition of the member function separately, then,
the header file myclass.h
would be:
假设您的成员函数是乘以您的类的特定成员。如果让头文件和源文件分别保存类的信息和成员函数的定义,那么头文件 myclass.h
就是:
#ifndef myclass_h
#define myclass_h
class myclass{
public:
int member1_;
double member2_;
myclass (){
member1_ = 1;
member2_ = 2.0;
}
myclass& MULT(int scalar);
myclass* MULTP(double scalar);
};
#endif
and the source file: myclass.cpp
would be:
和源文件:myclass.cpp
将是:
myclass& myclass::MULT(int scalar){
member1_ *= scalar;
return *this;
}
myclass* myclass::MULTP(double scalar){
member2_ *= scalar;
return this;
}
If you initialize an object called obj
, the default constructor above sets member1_
equal to 1:
Then in your main function, you can do chains such as:
如果您初始化一个名为 的对象obj
,则上面的默认构造函数设置为member1_
等于 1:然后在您的主函数中,您可以执行以下链式操作:
myclass obj;
obj.MULT(2).MULT(4);
Then member1_
would now be 8. Of course, the idea might be to chain different functions,
and alter different members.
那么member1_
现在是 8。当然,这个想法可能是链接不同的功能,并改变不同的成员。
In the case you are using the return by pointer, the first call uses the object, and any subsequent call will treat the previous result as a pointer, thus
如果您使用的是按指针返回,则第一次调用使用该对象,任何后续调用都会将前一个结果视为指针,因此
obj.MULTP(2.0)->MULTP(3.0);
回答by littleadv
Because the return type is void
, i.e.: you declare that you don't return anything. Change it to myclass*
to return this
change to myclass &
to return reference to the class through *this
.
因为返回类型是void
,即:您声明不返回任何内容。将其更改myclass*
为返回this
更改为myclass &
通过 返回对类的引用*this
。