const void time::gettime (); const; // what do each of the two const's do in this case??
const void time::settime(); // whats the difference in having const preceding this function compared to this: void time::settime() const; Deitel & Deitel have this. I think the first one is to make the function constant to the client. The second Im not sure about, but it was in the book.
#include <iostream.h> #include <stdlib.h> class time{ public: int a; time() { a=100; }; const time timex(time*x); void isonmyside(void) const; }; const time time::timex(time*x) { x->a=99; cout<<"The value of the variable a is "<<(*this).a<<endl<<" "<<x->a<<endl; return (*this); }; void time::isonmyside(void) const { time *n; //(*this).a=100 not allowed right? n=(time*)&(*this); n->a=899; cout<<"The value of the variable a is "<<(*this).a<<endl; return; }; int main(int argc,char**argv) { time t,w; int a,b; cout<<"Objects instanciated printing values now "<<t.a<<" "<<w.a<<endl; t.timex(&w); t.isonmyside(); cout<<"Main The value of time t.a is "<<t.a<<endl; cout<<"Main The value of time w.a is "<<w.a<<endl; system("pause"); return 0; };
Forgive me if I'm wrong ... (i've not been doing C++ in quite a few months, so I'm a little cold regarding its finer details), but isn't 'const' used to ensure that only one variable/object/etc can be in existence??
voivoid time::isonmyside(void) const { time *n; //(*this).a=100 not allowed right? n=(time*)&(*this); n->a=899; cout<<"The value of the variable a is "<<(*this).a<<endl; return; };
In it, it states that const is used to tell your compiler that a variable cannot be changed by the program. Therefore that implies that only one instance of that variable can exist during the lifecycle of the program, AND the value can't be altered
Originally posted by paddymee However I don't see how you imply that there can only be one instance in the whole lifecycle of the program because it's const. That sounds like a STATIC to me. Maybe you are confused there?