c++ 中的 `this` 运算符?

发布于 2024-10-14 20:11:01 字数 611 浏览 2 评论 0原文

抱歉,不确定以前是否有人问过这个问题,我真的不知道该查找什么。我是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

极度宠爱 2024-10-21 20:11:01

首先,您的 invertcolors() 函数定义是一个非成员函数。尽管您已在 Image 类中声明了它,但您尚未以任何方式将实现链接到该类,因此编译器认为它是非成员函数。要使其成为 Image 的成员,您需要像这样使用 Image::invertcolors

void Image::invertcolors(){
    int width=TellWidth();
    int height=TellHeight();
    ...
}

在 C++ 中您确实得到了 this,但它是一个指针,因此您必须在 C++ 中使用 this->getHeight() 。但请注意,在这种情况下它是多余的。作为初学者,您可能会发现具有与属性相同的参数名称的方法中唯一真正的用途。在这种情况下,您需要使用 this->height = height 。不过,请注意,C++ 在这里添加了很好的语法。此代码的作用与简单的 setter 相同:

void Image::setHeight(int height): height(height) {}

请注意,在 Java 和 C++ 中 this 都不是运算符。 .->+ 是运算符的示例。

First of all, your invertcolors() function definition is a non-member function. Although you've declared it inside the Image 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 of Image, you need to use Image::invertcolors like this:

void Image::invertcolors(){
    int width=TellWidth();
    int height=TellHeight();
    ...
}

You do get this in C++, but it's a pointer so you have to use this->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 use this->height = height for example. However, note that C++ has a nice syntax addition here. This code does the same as a simple setter:

void Image::setHeight(int height): height(height) {}

Note that neither in Java nor C++ is this an operator. ., -> and + are examples of operators.

﹎☆浅夏丿初晴 2024-10-21 20:11:01

this 是一个关键字,而不是一个运算符,它确实存在于 C++ 中。它是一个指针,因此在访问成员时,您将使用 -> 而不是 .

this is a keyword, not an operator, and it does exist in C++. It's a pointer, so you'll use it with ->, not ., when accessing members.

无言温柔 2024-10-21 20:11:01

在成员函数内部,this->whatever 是隐式的,因此您可以单独使用 whatever,编译器会发现您的意思是 this ->无论如何。在某些情况下(主要是在模板中),在 C++ 中使用 this-> 也是有意义的,但很少有必要(我知道有时,但在写完之后C++ 已经有几十年了,我可能仍然可以用手指来计算我完成它的次数)。

如果您的代码不在成员函数中,那么它必须显式引用某个特定对象(就像在 Java 中一样)。

Inside a member function, this->whatever is implicit, so you can just use whatever on its own, and the compiler will figure out that you mean this->whatever. There are a few cases (mostly in templates) that it can make sense to use this-> 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).

挽袖吟 2024-10-21 20:11:01

“this”在 C++ 中也可用。它是一个指向正在调用该函数的对象的指针。

"this" is also available in C++. It's a pointer to the object on which the function is being called.

泡沫很甜 2024-10-21 20:11:01

假设invertcolors()TellWidth()TellHeight()Image类的成员函数,它会像你写的那样工作。

在 C++ 中,this 是一个常量指针,指向调用该方法的对象(与 Java 不同,其中 this 是一个引用)。所以,

this.doSomething(); // in Java

你会说:

this->doSomething(); // in C++.

Assuming that invertcolors(), TellWidth(), and TellHeight() are member functions of the Image 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, where this is a reference). So, rather than:

this.doSomething(); // in Java

you'd say:

this->doSomething(); // in C++.
难得心□动 2024-10-21 20:11:01

编辑:抱歉,没有看到有关参数的部分。新答案:如果函数参数是不可协商的,并且您无法将 invertColors 定义为 Image 的成员函数(通过扩展 Image 类),那么您将需要一个全局定义的变量。

在主函数之外,声明 Image* myimg;

然后在函数中像平常一样使用 myimg 。

前任:

Image* myImg;
int main()
{
    ..all your initialization, etc. 
    invertColors();
}

void invertColors()
{
     int width = myImg->width;
     ...
}

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* myImg;
int main()
{
    ..all your initialization, etc. 
    invertColors();
}

void invertColors()
{
     int width = myImg->width;
     ...
}
靑春怀旧 2024-10-21 20:11:01

阅读评论,您是否缺少如何定义方法:

头文件:Image.h

class Image : public BMP {

public:

    void invertcolors();
};

源文件 Image.cpp

void Image::invertcolors()
{
    int width=TellWidth();
    int height=TellHeight();
    // ...
}

Reading the comments, are you missing how to define the method:

Header file: Image.h

class Image : public BMP {

public:

    void invertcolors();
};

Source file Image.cpp

void Image::invertcolors()
{
    int width=TellWidth();
    int height=TellHeight();
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文