c ++中的`this`运算符?(`this` operator in c++?)

编程入门 行业动态 更新时间:2024-10-27 04:32:36
c ++中的`this`运算符?(`this` operator in c++?)

很抱歉不确定之前是否有人询问,我真的不知道该怎么查。 我是Java的新手。 当我们想在Java中调用对象上的函数时,我们说picture.rotateRight();

然后,在rotateRight() ,我们有类似int height=this.getHeight(); 。 但是,我们如何在C ++中执行此操作? 我有一个名为invertcolors();的方法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.

最满意答案

首先,您的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.

更多推荐

本文发布于:2023-07-30 22:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   operator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!