c++ 中的 `this` 运算符?
抱歉,不确定以前是否有人问过这个问题,我真的不知道该查找什么。我是从 Java 转向 C++ 的新手。当我们想要在 Java 中调用对象上的函数时,我们会说 picture.rotateRight();
然后,在 rotateRight()
中,我们会得到类似 int height=this.getHeight();
。然而,我们如何在 C++ 中做到这一点呢?我有一个名为 invertcolors();
的方法,然后我有类似的内容:
Image* myImage = new Image();
bool b = myImage->ReadFromFile("in_01.bmp");
myImage->invertcolors();
void invertcolors(){
int width=TellWidth();
int height=TellHeight();
...
}
如何从方法定义访问 myImage
而无需实际说出 myImage
(因为该名称稍后可以更改)。
此外,函数参数是不可协商的。
Sorry not sure if this has been asked before, I really dont know what to look up either. I'm new to C++ from Java. When we want to call a function on an object in Java, we say picture.rotateRight();
Then, in rotateRight()
, we'd have something like int height=this.getHeight();
. However, how do we do this in C++? I have a method named invertcolors();
and then I have something like:
Image* myImage = new Image();
bool b = myImage->ReadFromFile("in_01.bmp");
myImage->invertcolors();
void invertcolors(){
int width=TellWidth();
int height=TellHeight();
...
}
How do I access myImage
from the method definition without actually saying myImage
(since that name can later be changed).
Also, the function parameters are non-negotiable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,您的
invertcolors()
函数定义是一个非成员函数。尽管您已在Image
类中声明了它,但您尚未以任何方式将实现链接到该类,因此编译器认为它是非成员函数。要使其成为Image
的成员,您需要像这样使用Image::invertcolors
:在 C++ 中您确实得到了
this
,但它是一个指针,因此您必须在 C++ 中使用this->getHeight()
。但请注意,在这种情况下它是多余的。作为初学者,您可能会发现具有与属性相同的参数名称的方法中唯一真正的用途。在这种情况下,您需要使用this->height = height
。不过,请注意,C++ 在这里添加了很好的语法。此代码的作用与简单的 setter 相同:请注意,在 Java 和 C++ 中
this
都不是运算符。.
、->
和+
是运算符的示例。First of all, your
invertcolors()
function definition is a non-member function. Although you've declared it inside theImage
class, you haven't linked the implementation to the class in any way so the compiler thinks its a non-member function. To make it a member ofImage
, you need to useImage::invertcolors
like this:You do get
this
in C++, but it's a pointer so you have to usethis->getHeight()
in C++. However, note that it is redundant in this case. As a beginner you'll probably find the only real use in a method having the same argument name as an attribute. In this case, you'll need to usethis->height = height
for example. However, note that C++ has a nice syntax addition here. This code does the same as a simple setter:Note that neither in Java nor C++ is
this
an operator..
,->
and+
are examples of operators.this
是一个关键字,而不是一个运算符
,它确实存在于 C++ 中。它是一个指针,因此在访问成员时,您将使用->
而不是.
。this
is a keyword, not anoperator
, and it does exist in C++. It's a pointer, so you'll use it with->
, not.
, when accessing members.在成员函数内部,
this->whatever
是隐式的,因此您可以单独使用whatever
,编译器会发现您的意思是this ->无论如何
。在某些情况下(主要是在模板中),在 C++ 中使用this->
也是有意义的,但很少有必要(我知道有时,但在写完之后C++ 已经有几十年了,我可能仍然可以用手指来计算我完成它的次数)。如果您的代码不在成员函数中,那么它必须显式引用某个特定对象(就像在 Java 中一样)。
Inside a member function,
this->whatever
is implicit, so you can just usewhatever
on its own, and the compiler will figure out that you meanthis->whatever
. There are a few cases (mostly in templates) that it can make sense to usethis->
in C++ as well, but it's only rarely necessary (I'm aware of the times, but after writing C++ for a couple of decades, I can probably still count the times I've done it on my fingers).If your code is not in a member function, then it has to explicitly refer to some particular object (much as in Java).
“this”在 C++ 中也可用。它是一个指向正在调用该函数的对象的指针。
"this" is also available in C++. It's a pointer to the object on which the function is being called.
假设
invertcolors()
、TellWidth()
和TellHeight()
是Image
类的成员函数,它会像你写的那样工作。在 C++ 中,
this
是一个常量指针,指向调用该方法的对象(与 Java 不同,其中this
是一个引用)。所以,你会说:
Assuming that
invertcolors()
,TellWidth()
, andTellHeight()
are member functions of theImage
class, it will work just as you've written it.In C++,
this
is a const-pointer to the object on which the method was invoked (as opposed to Java, wherethis
is a reference). So, rather than:you'd say:
编辑:抱歉,没有看到有关参数的部分。新答案:如果函数参数是不可协商的,并且您无法将 invertColors 定义为 Image 的成员函数(通过扩展 Image 类),那么您将需要一个全局定义的变量。
在主函数之外,声明 Image* myimg;
然后在函数中像平常一样使用 myimg 。
前任:
EDIT: Sorry, didn't see the part about the parameters. New Answer: If the function parameters are non-negotiable, and you can't be arsed to define invertColors as a member function for Image (by extending the Image class) then you'll need a globally defined variable.
Outside of your main function, declare Image* myimg;
then use myimg like normal inside your function.
ex:
阅读评论,您是否缺少如何定义方法:
头文件:Image.h
源文件 Image.cpp
Reading the comments, are you missing how to define the method:
Header file: Image.h
Source file Image.cpp