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