How to do it without a typedef

LordN3mrod's Avatar, Join Date: Sep 2010
Go4Expert Member
Hi,
there was a time when I was sure that a typedef is simply meant for programmer's convenience, and that any program could theoretically be written without using it.
For example, I came to learn how to declare variables and/or functions with arbitrarily complicated types without a typedef, e.g. a function taking a pointer to function taking a reference to an array of 3 pointers to functions .... blah blah blah. I can do this without a typedef. But! I came across something which I cannot do without a typedef. Let's consider a functor object that doesn't have an operator()

Code:
struct Square
{
   static int f(int n) {return n*n;}
   typedef int(*pf)(int);
   operator pf () (return &f;)
}; 
#include <iostream>
int main()
{
   Square s;
   std::cout << s(4); //Outputs 16
}
Now I REALLY REALLY wanna know if there is a way to rewrite operator pf without a typedef, like operator int(*)(int) () {...} (of course it doesn't work this was).

Any help will be appreciated
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Hmm very interesting! I wonder why this doesn't compile:
Code:
struct Square
{
   static int f(int n)
   {
	   return n*n;
   }
   static int f2(int n)
   {
	   return n+n;
   }
   typedef int(*pf)(int);
   typedef int(*pf2)(int);
   operator pf () 
   {
	   return &f;
   }
   operator pf2 () 
   { // <-- this line throws C2535
	   return &f2;
   }
};
Errors:
Code:
error C2535: 'Square::operator Square::pf(void)' : member function already defined or declared
see declaration of 'Square::operator Square::pf'
Edit: added code block to errors to prevent stupid smileys
0
LordN3mrod's Avatar, Join Date: Sep 2010
Go4Expert Member
why that code doesn't compile is obvious. You have defined the same member function twice.
pf and pf2 ARE NOT DISTICT TYPES!!! Therefore the two conversion operators are the same.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Ok smartass what about the OP's question.
0
LordN3mrod's Avatar, Join Date: Sep 2010
Go4Expert Member
whose? You mean my question ?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Note to self: pay more attention :-)