C++ operator overloading q.

S

suresh shenoy

Hello,

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

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

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

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

Hans Mull

suresh said:
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::eek:perator<< (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
 
A

acehreli

Hello,

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

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

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

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

#include <iostream>

using namespace std;

class CategoryStream;

/*
cspf is the name of a type. That type is a function pointer. That
type
can point to any function that takes a reference to CategoryStream
and
returns a reference CategoryStream.

Note: It is highly likely that the function returns the parameter
that it
takes.
*/
typedef CategoryStream& (*cspf) (CategoryStream&);

/*
This function can be pointed to by a cspf, because it takes a
reference
to CategoryStream, and returns a reference to a CategoryStream:
*/
CategoryStream & foo(CategoryStream & param)
{
/*
Normally, this function is expected to do something to param.
This is
similar to sending hex to cout as in

cout << hex;
*/
cout << "in foo\n";
return param;
}

class CategoryStream
{
public:

CategoryStream & operator<< (cspf);
};

// Here is how one might use foo:
void bar()
{
CategoryStream stream;

// ignoring the return value here
foo(stream);
}

CategoryStream& CategoryStream::eek:perator<< (cspf pf)
{
cout << "in operator<<\n";
return (*pf)(*this);
}

int main()
{
CategoryStream stream;
stream << foo;

/*
Because operator<< returns a reference to CategoryStream, we can
cascade any number of those:
*/
stream << foo << foo;
}
 
K

kwikius

Hello,

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

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

A typedef named cspf for a free function pointer , with argument
Categorystream & returning CategoryStream &.

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

Out of line definition of the << operator declared as a member of the
CategoryStream class.
operator<< function takes said function pointer as an argument. In the
function body, The function calls what ever function is input (pf)
with the class object (*this) as its arg and returns the result.

You should look through the faq which answers many similar basic C++
questions:

http://www.parashift.com/c++-faq-lite/

regards
Andy Little
 
S

suresh shenoy

#include <iostream>

using namespace std;

class CategoryStream;

/*
  cspf is the name of a type. That type is a function pointer. That
type
  can point to any function that takes a reference to CategoryStream
and
  returns a reference CategoryStream.

  Note: It is highly likely that the function returns the parameter
that it
  takes.
*/
typedef CategoryStream& (*cspf) (CategoryStream&);

/*
  This function can be pointed to by a cspf, because it takes a
reference
  to CategoryStream, and returns a reference to a CategoryStream:
*/
CategoryStream & foo(CategoryStream & param)
{
    /*
      Normally, this function is expected to do something to param.
This is
      similar to sending hex to cout as in

          cout << hex;
     */
    cout << "in foo\n";
    return param;

}

class CategoryStream
{
public:

    CategoryStream & operator<< (cspf);

};

// Here is how one might use foo:
void bar()
{
    CategoryStream stream;

    // ignoring the return value here
    foo(stream);

}

CategoryStream& CategoryStream::eek:perator<< (cspf pf)
{
    cout << "in operator<<\n";
    return (*pf)(*this);

}

int main()
{
    CategoryStream stream;
    stream << foo;

    /*
      Because operator<< returns a reference to CategoryStream, we can
      cascade any number of those:
    */
    stream << foo << foo;



}- Hide quoted text -

- Show quoted text -

I think you have explained it quite well :) . Why use typedef and not
just declare the function pointer? Thanks.
 
J

James Kanze

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::eek:perator<< (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::eek:stream&.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top