I am creating a socket output class so I can use << to send data to a socket. I've gotten the major parts to work and I am now onto adding in some other stuff I want like setw(), precision(), etc.
I've already gotten endl done by setting up an overloaded << to accept a pointer to a function, call the function, which then executes the proper code. All nice an easy.
My current problem is with trying to setup a precision(). I want to be able to do:
Code:
sendsock<<precision(5)<<20.837492;
but for the life of me I can not figure out how to setup the operator << to accept the correct parameters.
class defination:
Code:
class SocketOut {
int sock;
unsigned prec;
public:
SocketOut(int x) { sock = x; prec = 3;}
inline const SocketOut& operator << (
const SocketOut & (*f)(SocketOut &, unsigned int)
){
return f(*this,x);
}
inline const SocketOut& operator <<
(const SocketOut & (*f)(const SocketOut &) ) const{
return f(*this);
}
const SocketOut& operator << (unsigned char) const;
const SocketOut& operator << (unsigned char *) const;
const SocketOut& operator << (char x) const {
return (*this)<<(unsigned char)x;
}
const SocketOut& operator << (char *x) const{
return (*this)<<(unsigned char *)x;
}
const SocketOut& operator << (int) const;
const SocketOut& operator << (double) const;
const SocketOut& operator << (float x) const{
return (*this)<<(double)x;
}
const SocketOut& precision (unsigned x){
prec = x;
return *this;
}
};
Non-member functions that are used:
Code:
inline const SocketOut &endl (const SocketOut &x){
return x<<(unsigned char *)"\r\n";
}
inline const SocketOut &precision (SocketOut &x, unsigned int y) {
return x.precision(y);
}
The other member functions aren't really needed for this problem.
I've tried:
Code:
inline const SocketOut& operator << (
const SocketOut & (*f)(SocketOut &, unsigned int), unsigned x
){
return f(*this,x);
}
but it complains that operator << can only take one parameter. I've also tried various other attempts without success.
I know pointers to functions are one of my weaker areas so I would appreciate any help you could lend me.
Thanks