1 00:00:05,140 --> 00:00:07,640 As we've stepped through our code in the debugger, 2 00:00:07,640 --> 00:00:11,240 you've seen an identifier named this in the debugger pane. 3 00:00:12,040 --> 00:00:16,190 This is a reserved keyword that contains the address of the current object. 4 00:00:16,550 --> 00:00:20,950 So it's a pointer to the object that's currently being used by the class member methods. 5 00:00:21,310 --> 00:00:24,640 The this keyword can only be used within the scope of the class. 6 00:00:25,240 --> 00:00:27,940 In many other object-oriented programming languages, 7 00:00:27,940 --> 00:00:30,300 we use the word self instead of this. 8 00:00:30,600 --> 00:00:32,960 But in c++, we use the keyword this. 9 00:00:33,860 --> 00:00:36,860 All member access is done via the this pointer. 10 00:00:37,360 --> 00:00:40,760 However, c++ allows you to use member names directly. 11 00:00:40,760 --> 00:00:43,660 And behind the scenes, it's actually using this pointer. 12 00:00:44,020 --> 00:00:47,680 This can be used by the programmer, and in many cases, we must use it. 13 00:00:48,180 --> 00:00:51,480 We can use it to explicitly access data members and methods. 14 00:00:51,480 --> 00:00:54,680 We can also use it to determine if two objects are the same. 15 00:00:55,480 --> 00:00:58,480 We'll use this in the next section when we overload the assignment 16 00:00:58,480 --> 00:01:01,080 operator to determine if we're assigning to ourselves. 17 00:01:02,080 --> 00:01:06,280 Of course, since this points to the current object if you de-reference it, 18 00:01:06,280 --> 00:01:09,180 you'll follow the pointer and work with the current object. 19 00:01:11,380 --> 00:01:15,680 Here's an example of one way to write the set balance method for an account class. 20 00:01:15,680 --> 00:01:18,340 Notice that the name of the parameter is bal 21 00:01:18,340 --> 00:01:21,340 and the code in the body of the method is balance equals bal, 22 00:01:21,890 --> 00:01:26,040 which assigns the value of the parameter bal to the class data member balance. 23 00:01:26,400 --> 00:01:29,000 We don't have to write this arrow balance 24 00:01:29,000 --> 00:01:32,200 but that's exactly what the compiler is doing for us behind the scenes. 25 00:01:33,900 --> 00:01:36,890 Now suppose that the name of the parameter was balance. 26 00:01:36,890 --> 00:01:39,190 Now in the method body, the assignment statement 27 00:01:39,190 --> 00:01:42,550 balance equals balance would use the parameter balance for 28 00:01:42,550 --> 00:01:46,050 both of those identifiers instead of the data member balance. 29 00:01:46,050 --> 00:01:47,550 That's because of scope rules. 30 00:01:48,540 --> 00:01:51,530 Now this statement would not accomplish what we wanted to do. 31 00:01:51,530 --> 00:01:54,190 So in order to refer to the data member balance, 32 00:01:54,190 --> 00:01:57,550 we need to explicitly use this arrow balance. 33 00:01:58,910 --> 00:02:02,600 Finally, suppose we have a compare balance method in the account class 34 00:02:02,600 --> 00:02:05,100 that compares the balance of one object with another. 35 00:02:05,760 --> 00:02:10,020 We could check to see if we're comparing the same objects by comparing this 36 00:02:10,020 --> 00:02:11,620 and the address of other. 37 00:02:11,980 --> 00:02:14,780 If they're the same, then we're dealing with the same object. 38 00:02:15,330 --> 00:02:16,330 In this example, 39 00:02:16,330 --> 00:02:20,130 it wouldn't make much difference in our code since we're simply comparing balances. 40 00:02:20,130 --> 00:02:24,380 But if our compare logic is involved or computationally expensive, 41 00:02:24,380 --> 00:02:28,070 then a quick check to see if the objects are the same could help performance. 42 00:02:28,430 --> 00:02:32,530 We'll use the this pointer again in the operator overloading section of the course.