Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to do it without a typedef (http://www.go4expert.com/forums/typedef-t23203/)

LordN3mrod 5Sep2010 01:24

How to do it without a typedef
 
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

xpi0t0s 6Sep2010 19:31

Re: How to do it without a typedef
 
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

LordN3mrod 6Sep2010 19:47

Re: How to do it without a typedef
 
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. :)

xpi0t0s 6Sep2010 20:27

Re: How to do it without a typedef
 
Ok smartass what about the OP's question.

LordN3mrod 6Sep2010 20:38

Re: How to do it without a typedef
 
whose? You mean my question :)?

xpi0t0s 7Sep2010 11:16

Re: How to do it without a typedef
 
Note to self: pay more attention :-)


All times are GMT +5.5. The time now is 14:09.