setw() type of overloaded <<

This is a discussion on setw() type of overloaded << within the C++ Programming forums, part of the General Programming Boards category; I am creating a socket output class so I can use << to send data to a socket. I've gotten ...

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    setw() type of overloaded <<

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What you need is a function object to help you:
    Code:
    struct argmanip {
      unsigned int x;
    public:
      const SocketOut& (*f)(SocketOut&, unsigned int);
      argmanip(const SocketOut& (*func)(SocketOut&, unsigned int), unsigned int init)
        : f(func)
        , x(init)
      {}
    };
    
    inline const SocketOut&
    operator<<(
      argmanip& manip
      )
    {
      return manip.f(*this, manip.x);
    }
    Then you can write the manipulators that take arguments to return argmanip objects:
    Code:
    inline const SocketOut&
    precision(
      SocketOut&   x,
      unsigned int y
      )
    {
      return x.precision(y);
    }
    
    inline argmanip
    precision(
      unsigned int y
      )
    {
      return argmanip(precision, y);
    }
    My best code is written with the delete key.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks Prelude. That actually clarifies what iomanip.h was doing with its #defines for setw and such

    Oh:
    Code:
    struct argmanip {
      unsigned int x;
    public:
    go into the SocketOut class defination correct?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >go into the SocketOut class defination correct?
    It should be either global or have public access within SocketOut so that your non-member functions can have access to it.
    My best code is written with the delete key.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks again

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Gonna need a wee bit more help

    new definations (with unrelavent member functions removed )
    Code:
    class SocketOut;
    
    struct argmanip {
      unsigned int x;
    
      public:
        const SocketOut& (*f)(SocketOut&, unsigned int);
    
        argmanip(const SocketOut& (*func)(SocketOut&, unsigned int), unsigned int init)
              : x(init)
              , f(func)
           {}
    };
    
    class SocketOut {
      int sock;
      unsigned prec;
    
      public:
    
        SocketOut(int x) { sock = x; prec = 3;}
    
        inline const SocketOut& operator << (
             const argmanip& manip //Had to add const to suppress warnings
        ){
          return manip.f(*this,manip.x);
        }
    
        inline const SocketOut& operator <<
          (const SocketOut & (*f)(const SocketOut &) ) const{
            return f(*this);
        }
        const SocketOut& operator << (double) const;
    
        const SocketOut& operator << (float x) const{
          return (*this)<<(double)x;
        }
    };
    
    inline const SocketOut& precision( SocketOut& x, unsigned int y ){
      return x.precision(y);
    }
    
    inline argmanip precision( unsigned int y ){
          return argmanip(precision, y);
    }
    now this works fine for:
    Code:
    sendsock<<precision(2)<<1234.827;
    But i get the following warning for
    Code:
    sendsock<<12345.67890<<precision(2)<<1234.827;
    passing `const SocketOut' as `this' argument of `const class SocketOut & SocketOut:perator <<(const argmanip &)' discards qualifiers
    How would I cast away const in this situation?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How would I cast away const in this situation?
    If you're just going to cast it away then maybe you shouldn't have put it there in the first place. Try banning const from your return values in operator<<'s and the manipulators.
    My best code is written with the delete key.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You have to simplify things don't you
    Heh was just following the example from iostream

    Ok I did remove all the consts save one. If I didn't have:
    Code:
    inline SocketOut& operator<< ( const argmanip& manip ) /*...*/
    I recieved the following error:
    Code:
    game.cpp:22: initialization of non-const reference type `struct argmanip &'
    game.cpp:22: from rvalue of type `argmanip'
    socket.h:42: in passing argument 1 of `SocketOut::operator <<(argmanip &)
    But it all works now. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 11:35 PM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21