Overloading Operators

Pages: 12
and why there is some white space between Matrix and &, and between ostream and &
that doesn't make a difference, its just personal preference... from what i understand
Last edited on
to your question

consider the class below

1
2
3
4
5
6
7
8
class D{
private:
	int i;
public:
	D(int i ): i(i){}
	int get(){ return i;}
	int get()const { return i;}
};


the function which takes a const object of type D
1
2
3
int f(const D& i){
	return i.get();
}



and some code
1
2
3
D o(6);
cout<<f(o)<<endl; // this calls the function f which calls the const version of the get
cout<<o.get()<<endl;// this calls the normal version of get 


that is a safer way, but if you do not implement the normal version it is also ok, in this case
 
cout<<o.get(); // calls the const version , no other way 


and to your problem with ostream, the last way is to define the function as a friend member inside your class

friend ostream& operator<< (ostream& out, const Matrix& test);

and implement it in your source or header file

1
2
3
4
ostream& operator<< (ostream& out, const Matrix& test){
//do some thing
return out;
}


and might be without const, I am not sure try it yourself
Last edited on
Topic archived. No new replies allowed.
Pages: 12