I would love to be able to get a deeper understanding of this code from the book & I will arrange in FACTS that I think I know vs QUESTIONS to facilitate.
FACT1:
this->day; //Like saying address of object &holiday.........holiday.day; (if day was public)
&holiday = 0x7bfde0 //All 3 lines verify to same &holiday address...great!
(unsigned int*)this = 0x7bfde0
this = 0x7bfde0
FACT2:
I thought that when you instantiate an object that it makes it's own copies of the attributes AND functions, but then today I read that there is only 1x instance of functions and that all objects use that instance of the functions but have their copy of the attributes.
QUESTION1:
1A) Date& operator ++ () {.......return *this;}
If the object itself used the operator in the main, "++holiday;" then shouldn't the compiler be smart enough to let me use "return this;" which would send the address of the object & connect to the return "Date&"....they would both then be address value of object? I am still having a disconnect somehwere here to be able to fully understand WHY this is this way?
return this; //Gives error below
"[Error] invalid initialization of non-const reference of type 'Date&' from an rvalue of type 'Date*'"
We can send pointers to functions and have parameter on the function end accept the pass by reference "&", so why wouldn't this work? Can't assign an address from a right value pointer...why not if "this" has the address of the object as verified by the code?
Or is this just another one of those C++ rules, can NEVER assign a pointer to an address (no R-value)?
Book says can't assign pointers to arrays, but can arrays to pointers & I think that was about it.
cout << "this = " << this << endl; //0x7bfde0 & I did not have to use *this
&holiday = 0x7bfde0 //All 3 lines verify to same &holiday address....great!
(unsigned int*)this = 0x7bfde0
this = 0x7bfde0
1B) Does the "operator ++" create a copy of the object, no because there were no parameters sent to the operator by value or by pointer?
1C) Does the "return *this;" create a copy/clone of the object? Is there a clone somehwere created?
1D) Do all operators HAVE TO have a return value? In this case the compiler seems to understand & reference the holiday object, in that "++day;" in the operator worked & I don't really even need a return to the operator:
operator ++ () {
++day;
return 0;}
And I still get 12/26/2016. Is this bad programming & I should always just put a return in?
1D) return *this;
This means return the value contained in the pointer address....the value of the pointer address of the object, but what values does it REALLY hold? ALL the attributes (variables) values? And maybe a single pointer in there to the 1x copy of the functions?
Also how do I get the address of the location of the pointer &this in code?
QUESTION2:
How is this return possible as one was not specified, does the compiler just put one in?
operator const char*(){......return dateInString.c_str();}
When I manually put a return in, I get a compile error...
const char* operator const char*()
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
|
#include <iostream>
#include <sstream>
using namespace std;
class Date
{
private:
int month, day, year;
string dateInString;
public:
Date (int inMonth, int inDay, int inYear)
: month(inMonth), day(inDay), year(inYear){}
Date& operator ++ ()
{
++day;
cout << "************************" << endl;
cout << "(unsigned int*)this = " << (unsigned int*)this << endl;
//How to get adress of where pointer is stored at?
//cout << "(&this) = " << &this << endl;
cout << "this = " << this << endl;
cout << "(*this) = " << *this << endl;
cout << "************************" << endl;
return *this; //????????????????????????????
}
void DisplayDate()
{
cout << month << "/" << day << "/" << year << endl;
}
operator const char*()
{
ostringstream formattedDate;
formattedDate << month << "/" << day << "/" << year << endl;
dateInString = formattedDate.str();
return dateInString.c_str();
}
};
int main()
{
Date holiday (12, 25, 2016);
cout << "&holiday = " << &holiday << endl;
holiday.DisplayDate();
++holiday;
holiday.DisplayDate();
return 0;
}
|