T H I S S.O.S. ! ! !

Hello!
Please, can someone write easiest example using keyword "this"? Many thnaks!!!

this->MemberVariable = variable;
Please, some (easy) pseudocode / or example where we can use "this"!
many thanks!!!
PleasE, some task form your prctiing book where solutionis made using using "this". Please, JUST A TASK, no code! MANY THANKS!!!
Last edited on
Ive got no idea what you're talking about man, a task that requires this as a solution? Sure...

You have a class, call it Foo. Foo has a constructor which takes 2 parameters.

Foo(int x, int y);

int x and int y are private.

Now, You need to write the declaration of this constructor. The constructors definition has to look like this

1
2
3
4
Foo(int x, int y)
{
      //set x and y here
}


Now. Your job is to take the x and y that was sent in, and make them equal to the x and y you have as private variables.
Last edited on
Lets presume that you have made a type class Foo. The Foo has logical property that can be incremented.

Your task is to write canonical pre-increment and post-increment operators for class Foo.
Hello!
a task that requires this as a solution? <- YES, RIGHT THIS!!!

I just forgot to mention, I would like to have more then just one object. So that I can differ the "entrance " to "this " from various objects.

Last edited on
MANY THANKS!!! IT WORKED!!!

I got:
http://codepad.org/BYPU1V5U

and:
http://codepad.org/VsKTuGOt

Please, is there a situation where it is not the same to use this->price or just price?

-> or is it ALWAYS the same?

Any ides for example pseudocode?

MANY THANKS!!!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

struct Foo {
    int x, y;
    void setPos(int x, int y) {
        x = x;
        y = y;
    }
    
    void setThisPos(int x, int y) {
        this->x = x;
        this->y = y;
    }
};

int main() {
    Foo f = {1,1};
    f.setPos(3,4);
    std::cout << f.x << " " << f.y << std::endl;
    
    f.setThisPos(3,4);
    std::cout << f.x << " " << f.y << std::endl;

    return 0;
}
Last edited on
Another example using this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
BigInt& BigInt::operator-=(const BigInt &rhs)
{
    if (positive != rhs.positive)
    {
        BigInt temp(rhs);
        temp.positive = !temp.positive;
        return operator+=(temp);
    }
    else if (positive)
    {
        if (*this < rhs)
        {
            BigInt temp(rhs);
            temp -= *this;
            temp.positive = !temp.positive;
            *this = temp;
            return *this;
        }
    }
    else if (*this > rhs)
    {
        BigInt temp(rhs);
        temp -= *this;
        temp.positive = !temp.positive;
        *this = temp;
        return *this;
    }
    //actual calculations here
    return *this;
}
closed account (D80DSL3A)
And yet another...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bigInt& bigInt::operator+=( const bigInt& b )
{
    if( isPositive )
    {
        if( b.isPositive )
            this->add( b );// lhs calling function
        else
            this->subtract( b );// actual calcs in these functions
    }
    else// A is negative
    {
        if( b.isPositive )
            this->subtract( b );
        else
            this->add( b );
    }

    return *this;
}
Last edited on
Thanks!!!
There are many reasons to use this. Here are some:

* Ambiguity resolution. Member function has a parameter with the same name as member variable of the class. Within the function scope the parameter hides the member, but the member can be accessed with this. However, having such name clashes is IMHO bad style.

* Code clarity. bool Foo::equalX( const Foo & other ) const { return (this->x == other.x); } No syntactic ambiguity, but keep things clear for the reader.

* Return (reference to) object or its address. The pre-increment operator is an example.

* RTTI (runtime type information). See how and why Visitor design pattern accepts this.
Many thank!!!!!!!
Topic archived. No new replies allowed.