keyword this not printing

hi guys from my understanding the keyword this is a pointer that points to the current object in scope for example currentObject.value;

but when I try to use the this keyword in my program nothing is happening it does not print the variable age,it compiles but does not print anything

and side question when I'm returning *this what m I actually returning(yes I understand how pointers work and have read countless of tutorials and chapters on them but this confuses me because you are using an object)

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  #include <iostream>

using namespace std;


class foo{

   public:


   int age;
   int number;
   int *ray;

    // defult

    foo(){

        age = 24;
        number = 5;
        ray = new int[5];

        for(int i =0; i < 5; i++){

            ray[i] = i;

        }

    }

    // copy

    ~foo(){

        delete [] ray;

    }

    foo(const foo &f){


       age = f.age;
       number = f.number;
       ray = new int[5];

       for(int i = 0; i < 5; i++){

           ray[i] = f.ray[i];

       }


    }

    foo& operator=(const foo& f){


       if(this == &f){


         return *this;

       }else{

         age = f.age;
         number = f.number;
         ray = new int[5];

         for(int i = 0;i < 5;i++){

            ray[i] = f.ray[i];


         }

         cout << this->age << endl; // does nothing??

         return *this;



       }


    }
Are you sure you're even calling the assignment operator=?

When I create a small main() that declares a couple of instances of the class then use the assignment operator the program prints the age.

so your saying that the copy constructor may have got called instead?
Yes.
thanks that makes sense
Topic archived. No new replies allowed.