I'm using mingw gcc(3.xxx) compiler. In the following code I pass a variable 'q'as 'const' but I later use its private members in the '>>'operator (in the same function) which obviously modify it. but the compiler did'nt complain and the programm compiled. Later during execution when I used the '>>' operation, the program halted when it reached that point. Even though I understood the error and corrected it, what I don't understand is why the compiler did'nt yell at me.
Does someone have a clue?
Code://quaternion.h //no #include iostream as it is included in the main file. using namespace std; //for the istream and ostream class Quaternion{ private: long double a, b, c, d; public: Quaternion(long double aa=0, long double bb=0, long double cc=0, long double dd=0):a(aa), b(bb), c(cc), d(dd){} virtual ~Quaternion(){} friend ostream& operator <<(ostream& out, const Quaternion& q){ out<<q.a<<"+"<<q.b<<"i+"<<q.c<<"j+"<<q.d<<"k"; return out; } friend istream& operator >>(istream& in, constQuaternion& q){ in>>q.a>>q.b>>q.c>>q.d;//modifies q's members return in; } Quaternion operator +(const Quaternion q){ return Quaternion(a+q.a, b+q.b, c+q.c, d+q.d); } Quaternion operator -(const Quaternion q){ return Quaternion(a-q.a, b-q.b, c-q.c, d-q.d); } void set(long double aa=0, long double bb=0, long double cc=0, long double dd=0){ //blah blah } long double& operator [](unsigned int i){ //0 for 'a' component //blah blah } Quaternion operator *(const Quaternion& q){ //blahblah } };Code://main.cpp #include<iostream> #include"../quaternionfractal/quaternion.h" using namespace std; int main(){ Quaternion x, y; cout<<"x :"; [b]cin>>x; //when here the program halts and terminates without any complaining. cout<<"y :"; cin>>y; cout<<"x :"<<x<<"\n" <<"y :"<<y<<"\n" <<"x*y :"<<x*y<<"\n" <<"y*x :"<<y*x<<"\n"; return 0;[b] }



LinkBack URL
About LinkBacks


