By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,462 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,462 IT Pros & Developers. It's quick & easy.

In Class Typedefs and In Class functions pointers.

P: n/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.
Mar 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
Mar 23 '07 #2

P: n/a
asd
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.

Mar 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.