Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importance of "this"

i'm programming in c++ using cocos2dx and when adding a sprite or objects,

this-> addChild(something) and addChild(something)

works. Both ways come up with same result.

But not only in c++, this is used in android programming too (perhaps, all programming languages use "this"?). I've used this in Java to replace ClassName.class, with simple this. But besides this purpose this is used in many ways.

But because I haven't gone deep into programming languages, I don't know if they really do the same job (this-> addChild vs. addChild AND ClassName.class vs. this).

Why do we need this?

.

.

Self Answer:

this is a keyword that refers to the current class instance or object in many object-oriented programming languages.

so...just for comfort?

like image 666
user3290525 Avatar asked Mar 05 '14 04:03

user3290525


People also ask

What is the important of importance?

The importance of something is its quality of being significant, valued, or necessary in a particular situation. Safety is of paramount importance. Importance means having influence, power, or status.

What is the importance of the meaning?

Meaning serves a number of important functions in human lives (Frankl, 1992). Firstly, meaning provides a purpose for our lives. Secondly, it furnishes values or standards by which to judge our actions. Thirdly, it gives us a sense of control over the events in our life.

How can I use importance in a sentence?

"His research has tremendous importance for cancer patients." "He noticed the declining importance of his work." "Her added importance to the group gave her much confidence." "It is of relative importance to him."


3 Answers

this has a few uses. First, in some cases, you will need to explicitly refer to the receiver object in the current method, perhaps if you're passing it as a parameter to another function. For example, consider this C++ code:

void someOtherFunction(SomeClass* arg);

void SomeClass::doSomething() {
    someOtherFunction(this);
}

Here, someOtherFunction is a free function that takes a pointer to a SomeClass object. The only way to call it in the context of doSomething so that the receiver object is the parameter is to use this.

this in C++ is also used in assignment operators, which need to return a reference to the receiver object:

MyClass& MyClass::operator= (MyClass rhs) {
    // Do something to the receiver object
    return *this;
}

Here, return *this; means "return a reference to the receiver object," which couldn't be expressed without this.

this is also used to disambiguate in a few circumstances. For example:

public class MyClass {
    private int value;

    public MyClass(int value) {
        this.value = value;
    }
}

Here, the use of this in the MyClass constructor is to differentiate between the parameter value and the field value. Referring to value by itself selects the parameter, while using this.value refers to the field.

Hope this helps!

like image 112
templatetypedef Avatar answered Sep 26 '22 14:09

templatetypedef


consider also a normal type of constructor or setter

public ConstrcutMe (String name, int age)
{
    this.name = name;
    this.age = age;
}

public void setName (String name) {
{
   this.name = name;
}

I believe this fully illustrate why we need this

like image 29
Scary Wombat Avatar answered Sep 24 '22 14:09

Scary Wombat


In C++, this is a keyword which evaluates to a pointer to the object the method is being called on.

Strictly for convenience, the compiler can figure out, in some cases, where you want to call another method on the same object. If the object has a fooBar method, then you can write it as this->fooBar();, or for convenience you can write just fooBar(); and the compiler will understand it as this->fooBar();.

However, in other cases you really do need to use this explicitly. Consider if you want the method to return the instance itself. You have to refer to the instance itself somehow, which you can do using return this;. Or, if you want to call another function, say barMan, passing in the instance itself - again you need to use this: barMan(this);.

like image 38
Claudiu Avatar answered Sep 24 '22 14:09

Claudiu