1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to do it without a typedef

Discussion in 'C' started by LordN3mrod, Sep 4, 2010.

  1. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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
     
  3. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    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. :)
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Ok smartass what about the OP's question.
     
  5. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    whose? You mean my question :)?
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Note to self: pay more attention :)
     

Share This Page