classes and 'this' keyword question

I have few questions.

1. What is this called? what would it do for the code ?

I found these information:


"in C++, the "this" keyword is a pointer to the current instance of the class in a function in which the "this" is used".

"this is just a pointer to the object that a function is working on".



2. What does this part of the code do ?
1
2
3
		if (this == &other){
			return *this;
		}


3. What would this part of the code return ?
 
                return *this;



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	test& operator=(const int_vec& use){
		// self assignment is a special case:
		if (this == &use){
			return *this;
		}
		else {

			// resize this int_vecs underlying array if necessary
			if (capacity < use.size){
				resize(use.size + 10);
			}

			size = use.size;

			for (int i = 0; i < size; i++){
				arr[i] = use.arr[i];
			}
			return *this;
		}
	}
]
Last edited on
2. What does this part of the code do ?
1
2
3
if (this == &other){
	return *this;
}


Most likely to prevent self-assignment or to prevent self-operations on the object itself.
Could you please clarify what you mean by self-assignment or preventing self operations ?

I didn't write the code, my teacher gave it as an example, so I have a little hard time understanding what is what.
Self assignment is something like this :

A a; a = a;

Self operation is something like this :

A a; a.doSomething(&a); // doSomething takes a pointer

Most of the time, we don't do self assignments or self operations intentionally. But things like this may occur. And when they occur, it may be just a waste of time since the two objects are exactly the same or just itself, for example a Vector just literally re-copies the contents of itself, which is not needed. Imagine if the Vector has 10000000 elements, that is a waste of computing power. That is why code to check for self-assignment or self-operation may be needed some time to prevent such accidents.
Last edited on
The this pointer

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

struct A
{
    void foo() const
    { std::cout << "A::foo called on object at address " << this << "\n\n" ; }

    A& bar( const A& that )
    {
        std::cout << "A::bar called on object at address " << this << '\n'
                  << "the address of the object 'that' is " << std::addressof(that) << '\n' ;

        if( this == std::addressof(that) ) std::cout << "it is the same object\n\n" ;
        else std::cout << "these are two different objects\n\n" ;

        A& result = *this ; // this a a pointer of type A*
                            // *this is an lvaule of type A
                            // result is a reference to the object on which bar was called

        return result ; // (return *this): return reference to the object on which bar was called
    }
};

int main()
{
    A one ;
    std::cout << "address of one is: " << std::addressof(one) << '\n'
              << "call one.foo()\n" ;
    one.foo() ;

    static A two ;
    std::cout << "address of two is: " << std::addressof(two) << '\n'
              << "call two.foo()\n" ;
    two.foo() ;

    A* p_three = new A ;
    std::cout << "address of object (value of p_three) is: " << p_three << '\n'
              << "call p_three->foo()\n" ;
    p_three->foo() ;
    delete p_three ;

    std::cout << "call one.bar(two)\n" ;
    one.bar(two) ;

    std::cout << "call one.bar(one)\n" ;
    one.bar(one) ;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
address of one is: 0x7fff375edb38
call one.foo()
A::foo called on object at address 0x7fff375edb38

address of two is: 0x615539
call two.foo()
A::foo called on object at address 0x615539

address of object (value of p_three) is: 0x10dbc30
call p_three->foo()
A::foo called on object at address 0x10dbc30

call one.bar(two)
A::bar called on object at address 0x7fff375edb38
the address of the object 'that' is 0x615539
these are two different objects

call one.bar(one)
A::bar called on object at address 0x7fff375edb38
the address of the object 'that' is 0x7fff375edb38
it is the same object

http://coliru.stacked-crooked.com/a/68cc113f041fe425
Topic archived. No new replies allowed.