How to do it without a typedef

Go4Expert Member
5Sep2010,01:24   #1
LordN3mrod's Avatar
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
Mentor
6Sep2010,19:31   #2
xpi0t0s's Avatar
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
Go4Expert Member
6Sep2010,19:47   #3
LordN3mrod's Avatar
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.
Mentor
6Sep2010,20:27   #4
xpi0t0s's Avatar
Ok smartass what about the OP's question.
Go4Expert Member
6Sep2010,20:38   #5
LordN3mrod's Avatar
whose? You mean my question ?
Mentor
7Sep2010,11:16   #6
xpi0t0s's Avatar
Note to self: pay more attention :-)