In Class Typedefs and In Class functions pointers.

A

asd

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.
 
I

Ian Collins

asd said:
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....
 
A

asd

The only thing wrong here is the lack of a return.
Yes, you did it the correct way:

Expression::int_function.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top