Snowman
Snowman

Reputation: 32061

`this` operator in c++?

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.

Upvotes: 0

Views: 242

Answers (7)

Keith
Keith

Reputation: 6834

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();
    // ...
}

Upvotes: 0

AndyG
AndyG

Reputation: 41092

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;
     ...
}

Upvotes: 0

moinudin
moinudin

Reputation: 138317

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.

Upvotes: 5

Ben Voigt
Ben Voigt

Reputation: 283624

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.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490048

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).

Upvotes: 1

Drew Hall
Drew Hall

Reputation: 29047

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++.

Upvotes: 0

Babak Naffas
Babak Naffas

Reputation: 12561

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

Upvotes: 0

Related Questions