
(member) function returning pointer to functions like itself?
Quote:
> > > T t;
> > > ...
> > > t = (*t) (); // I really want it like this
> > You cannot do that. You're calling a function using
> > undefined pointer. Declaration T t; leaves 't' uninitialised.
> I didn't put ellipsis there for nothing.
> > I strongly doubt it is possible. It's infinitely recursive.
> It would've been possible if I could write
> typedef T* (*T) ();
That would be a pointer to a pointer to a function, wouldn't it?
Quote:
> but nor C neither C++ allow me do that. On the other hand, I can use
> things like
> class T {
> ...
> class T f();
> // or
> class T* g ();
> ...
> };
In C++ (and forgive me for still cross-posting in comp.lang.c)
you can do this:
===================================================
#include <iostream>
using std::cout;
using std::endl;
class T
{
T (*function)();
public:
T() : function(0) {}
T(T (*a)()) : function(a) {}
const T& operator=(const T& t) { function = t.function; return *this; }
T operator()() { if (function) return function(); else return *this; }
///// this is just for running this example
friend std::ostream& operator <<(std::ostream& s, const T&);
Quote:
};
///// this is just for running this example
std::ostream& operator << (std::ostream& s, const T& t)
{
return s << t.function;
Quote:
}
T t1();
T t2();
T t1()
{
return t2;
Quote:
}
T t2()
{
return t1;
Quote:
}
int main()
{
T t = t1;
std::cout << t << std::endl;
t = t();
std::cout << t << std::endl;
t = t();
std::cout << t << std::endl;
return 0;
Quote:
}
===================================================
Quote:
> It's good enough for a workaround, but it doesn't give me the same
> performance (at least with g++) as I would've got if I were using
> pointers to functions. Besides, it's not C.
> > Victor
> Dima
Victor
--
Please remove capital A's from my address when replying by mail