C++ operator overloading q.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • suresh shenoy

    C++ operator overloading q.

    Hello,

    I came across a snippet of code which I was not able to interpret
    correctly.

    typedef CategoryStream& (*cspf) (CategoryStream &);

    CategoryStream& CategoryStream: :operator<< (cspf pf) {
    return (*pf)(*this);
    }

    If anyone could explain each line, it would be great!

  • Hans Mull

    #2
    Re: C++ operator overloading q.

    suresh shenoy schrieb:
    Hello,
    >
    I came across a snippet of code which I was not able to interpret
    correctly.
    I think this is to be interpreted as follows (I'm not really sure about
    the typedef):
    >
    typedef CategoryStream& (*cspf) (CategoryStream &);
    That defines a Reference to CategoryStream as a dereference of cspf
    (probably something defined by the library
    >
    CategoryStream& CategoryStream: :operator<< (cspf pf) {
    return (*pf)(*this);
    }
    This overloads an operator so the compiler can interpret the following code:
    CategoryStream stream;
    stream << pf; //pf is a cspf Instance; statement returns a
    //CategoryStream&
    //This is equivalent to writing (*pf)(*this)
    >
    If anyone could explain each line, it would be great!
    >
    I hope this is right. Kind reagrds, Hans

    Comment

    • James Kanze

      #3
      Re: C++ operator overloading q.

      On Feb 8, 7:57 pm, suresh shenoy <msureshshe...@ gmail.comwrote:
      I came across a snippet of code which I was not able to interpret
      correctly.
      typedef CategoryStream& (*cspf) (CategoryStream &);
      Declares cspf as a pointer to a function which returns a
      CategoryStream& , and has a single parameter of type
      CategoryStream& .
      CategoryStream& CategoryStream: :operator<< (cspf pf) {
      return (*pf)(*this);
      }
      First, this is the implementation of a member function; it will
      be called in expressions like:

      CategoryStream s ;
      s << someFunction ;
      If anyone could explain each line, it would be great!
      It's the standard idiom for a manipulator not taking any
      arguments in a stream. See, for example, things like std::hex,
      std::uppercase, etc., and the way they work in std::ostream&.

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...