In Class Typedefs and In Class functions pointers.

Discussion in 'C++' started by asd, Mar 23, 2007.

  1. asd

    asd Guest

    Hello All,

    I am going back to C++ after 3 years and I am struggling with it.
    FYI, This topic has 2 questions.

    Here is my problem. I have class, which must have a function which returns
    a pointer to a function.
    Here is how I did it.


    /////// in .h ///////////
    class Expression : public BaseType{
    public:
    typedef int (*int_function)(Expression *left, Expression *right, Gpl_type
    type);

    int_function GetIntFunction(yytokentype token);
    };

    /////// I tried following in .cpp ///////////

    int_function Expression::GetIntFunction(yytokentype token)
    {} // compiler barks since int_function is not in global scope

    Expression::int_function Expression::GetIntFunction(yytokentype token)
    {} // compiler barks saying that there is no int_function.



    /////////////////////////////////////////////////
    My first question is: Is there a way to access int_function typedef
    in the cpp file?

    I can not typedef int_function before the class definition because it has
    a pointer to Expression class.





    Since compiler did not let me use typedef outside of the scope I did
    something like following:

    /////// in .h ///////////
    class Expression : public BaseType{
    public:
    int (*GetIntFunction(yytokentype token))(Expression *left, Expression
    *right, Gpl_type type);
    // I could not remember the original statement but it was close to this
    // one and compiler said that it was ok.
    };


    /////// I tried following in .cpp ///////////
    int (*Expression::GetIntFunction(yytokentype token))(Expression *left,
    Expression *right, Gpl_type type); // compiler barks.

    int (Expression::*GetIntFunction(yytokentype token))(Expression *left,
    Expression *right, Gpl_type type); // compiler barks.



    My second question is: How should i declare that function in the cpp file
    so the compiler will not bark?



    I appreciate your efforts and time for answering my silly questions.
    Right now I solved the problem by using same typedef inside and after the
    definition of the class. I am just trying to learn what is the proper way
    of doing it.
    Thanks a lot.
     
    asd, Mar 23, 2007
    #1
    1. Advertising

  2. asd

    Ian Collins Guest

    asd wrote:
    > Hello All,
    >
    > I am going back to C++ after 3 years and I am struggling with it.
    > FYI, This topic has 2 questions.
    >
    > Here is my problem. I have class, which must have a function which returns
    > a pointer to a function.
    > Here is how I did it.
    >
    >
    > /////// in .h ///////////
    > class Expression : public BaseType{
    > public:
    > typedef int (*int_function)(Expression *left, Expression *right, Gpl_type
    > type);
    >
    > int_function GetIntFunction(yytokentype token);
    > };
    >
    > /////// I tried following in .cpp ///////////
    >
    > int_function Expression::GetIntFunction(yytokentype token)
    > {} // compiler barks since int_function is not in global scope
    >
    > Expression::int_function Expression::GetIntFunction(yytokentype token)
    > {} // compiler barks saying that there is no int_function.
    >

    The only thing wrong here is the lack of a return.
    >
    >
    > /////////////////////////////////////////////////
    > My first question is: Is there a way to access int_function typedef
    > in the cpp file?
    >

    Yes, you did it the correct way:

    Expression::int_function.

    >
    > My second question is: How should i declare that function in the cpp file
    > so the compiler will not bark?
    >

    You have, assuming you included the header file in the source file....

    --
    Ian Collins.
     
    Ian Collins, Mar 23, 2007
    #2
    1. Advertising

  3. asd

    asd Guest

    On Fri, 23 Mar 2007 14:18:16 +1200, Ian Collins wrote:

    > asd wrote:
    >> Hello All,
    >>
    >> I am going back to C++ after 3 years and I am struggling with it.
    >> FYI, This topic has 2 questions.
    >>
    >> Here is my problem. I have class, which must have a function which returns
    >> a pointer to a function.
    >> Here is how I did it.
    >>
    >>
    >> /////// in .h ///////////
    >> class Expression : public BaseType{
    >> public:
    >> typedef int (*int_function)(Expression *left, Expression *right, Gpl_type
    >> type);
    >>
    >> int_function GetIntFunction(yytokentype token);
    >> };
    >>
    >> /////// I tried following in .cpp ///////////
    >>
    >> int_function Expression::GetIntFunction(yytokentype token)
    >> {} // compiler barks since int_function is not in global scope
    >>
    >> Expression::int_function Expression::GetIntFunction(yytokentype token)
    >> {} // compiler barks saying that there is no int_function.
    >>

    > The only thing wrong here is the lack of a return.
    >>
    >>
    >> /////////////////////////////////////////////////
    >> My first question is: Is there a way to access int_function typedef
    >> in the cpp file?
    >>

    > Yes, you did it the correct way:
    >
    > Expression::int_function.
    >
    >>
    >> My second question is: How should i declare that function in the cpp file
    >> so the compiler will not bark?
    >>

    > You have, assuming you included the header file in the source file....



    Thank you so much for your time Ian.
    I tried doing it again. They all work now. May be I had a typo when i
    first did it.
    Thank you so much again.
     
    asd, Mar 23, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. emerth
    Replies:
    3
    Views:
    392
    emerth
    Aug 8, 2003
  2. Marc Thrun
    Replies:
    15
    Views:
    884
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    327
    August Karlstrom
    Sep 24, 2006
  4. Ivan A. Kosarev
    Replies:
    0
    Views:
    374
    Ivan A. Kosarev
    Jan 13, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    737
Loading...

Share This Page