1
#include<iostream.h>
#include<conio.h>

typedef ostream& (*T)(ostream& , int);
class Base
{
    T fun;
    int  var;
    public:
    Base(T func, int arg): fun(func) , var(arg)
    {};
    friend ostream& operator<<(ostream& o, Base& obj)
    {
        return obj.fun(o,obj.var);
    }
};

ostream& odisp(ostream& o, int i);
{
    o<<”i=”<<i<<endl;
    return o;
}
Base disp(int i)
{
    return base(odisp, i)
};

I am totally stuck since the first line since i cant figure out how this typedef works.. it looks like a pointer to function syntax but again ostream has me puzzled..?? pls do explain how this whole code works..??

Zaffy
  • 16,801
  • 8
  • 50
  • 77
  • 4
    `iostream.h` is long gone. Use `iostream` instead and prefix everything in it with `std::`. While I'm at it, `conio.h` is very old and deprecated as well. Neither of those are even mentioned in the spec. Anyway, `std::ostream` is the type of `std::cout`. Replace it with any other type if that helps. – chris Jan 06 '13 at 08:33

1 Answers1

5
#include <iostream.h>
#include <conio.h>

As it has been pointed out, iostream.h and conio.h are not standard headers for C++ programs. The correct header include statement for the IO streams library is #include <iostream>, without the extension.

typedef ostream& (*T)(ostream& , int);

This creates a typedef named T which is a pointer to a function with these properties:

  • returns a reference to ostream
  • accepts two parameters, in this order:
    • a reference to an ostream
    • int value

ostream, assuming it's referring to std::ostream, is itself a typedef of std::basic_ostream, which is found in the C++ standard library. It is defined as:

namespace std {
    typedef basic_ostream<char> ostream;
}

Objects std::cout are instances of std::ostream.

class Base
{
    T fun; // (1)
    int var; // (2)
public:
    Base(T func, int arg): fun(func) , var(arg) {}; // (3)

    friend ostream& operator<<(ostream& o, Base& obj) // (4)
    {
        return obj.fun(o, obj.var);
    }
};

This is a class that holds two things: (1) a pointer to a function as described above and (2) an integer. The constructor (3) allows users of the class to instantiate it with a function pointer and an integer.

The friend function declaration (4) overloads the left bitshift operator <<. This sort of overloading is called operator overloading. By convention, The operator << is also called the stream insertion operator in the context of IO streams.

Note that friend functions defined this way are actually not members of Base, and thus do not receive a this pointer (hence, the need for a separate Base& parameter).

ostream& odisp(ostream& o, int i);
{
    o << "i=" << i << endl;
    return o;
}

This is a function called odisp. It takes in a reference to an ostream and an integer. If you pass in the integer 42, it prints out the passed-in integer in this form:

i=42

Base disp(int i)
{
    return base(odisp, i)
};

This function has multiple syntax errors and will not run as-is:

  • base is probably meant to be Base. In this case it would construct a Base temporary and returns it.
  • Missing a semicolon.

These issues are somewhat fundamental to the language. You may want to pick up a good introductory C++ book which will cover these issues.

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
  • As a note to avoid confusion, `operator<<` is the left bit shift operator, but it is also commonly known as the *stream insertion operator* when used for this purpose. Since it was very early on that this happened, it's become a sort of standard name for the operator, but it still really is the left bit shift operator in disguise. – chris Jan 06 '13 at 08:57
  • nice answer except for a little error at the end: `return Base(odisp,i);` would be the correct call to the constructor of `Base` with `odisp` the function (taken via function pointer). (I don't understand, though, why `Base` is called "Base".) – Walter Jan 06 '13 at 10:41