hi again all of my bros and dear experts here
i know whats this doing totally i know its memory address of our object and point on it but in point to something like overloading operators it will make me confuse that really whats the exact function done by its magic.
i mean like block of code below
i defined an operator overloading as a reference and use once for pre and in second for post increment but Please tell me really what exactly is happening and returning by this pointer.and why in our post increment we ddnt return a refrence just a value have to be return for proper function of post increment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class date{
public:
date &operator++();
date operator++(int);
};
date &date::operator ++()//pre increment
{
return *this;
}
date date::operator ++(int)//post increment
{
date temp=*this;
return temp;
}
It should work just like any other pointer. 'this' is address of your object and '*this' is your object, though what the latter would return exactly I'm not sure.
#include <iostream>
class Test
{
public:
// Return the pointer of your object
Test* Return1()
{
returnthis;
}
// Dereference the pointer and return the address of your object
Test* Return2()
{
return &(*this);
}
};
int main()
{
Test test;
std::cout << test.Return1() << std::endl;
std::cout << test.Return2() << std::endl;
return 0;
}
hi bro thanx for reply
i mean exact what object is returning please clarify for me.
we just put return this on post increment its returning our operand for doing operation?
It's returning the object you are operating on. So in the case of:
1 2
date obj;
obj++;
The this pointer points to obj.
The return statement inside the pre-increment operator is returning the object after it's been incremented, allowing you to then do something else with the object if you wanted to, such as assign it to something.
1 2
date obj;
date something_else = obj++; // possible because operator++() returns a date object, which we can then assign
thanx mate and what about post increment.
idea is "this" get our obj and assign it to some temp of our class type and do operation things on it then after increment, return value of incremented obj?that's right bro?