473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

In Class Typedefs and In Class functions pointers.

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
2 1793
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
2
by: Jason | last post by:
Hello, I have a class, transCore, that does certain work, and by default, prints its progress to the stand output. Later, I will to write a GUI class that encapsulate the transCore class. I...
11
by: James Aguilar | last post by:
In the C++ STL, there are various places where you can call a function that takes a functor as a parameter. This functor may be either a function object or a function pointer. For instance, if...
6
by: Erica | last post by:
Hi, I am currently working on a Pascal-to-C translation, and I am getting an error that I can't seem to debug. I have a globals.h file, and here is a snippet from it: -- typedef char...
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
70
by: garyusenet | last post by:
I'm using an example piece of code: - namespace Wintellect.Interop.Sound{ using System; using System.Runtime.InteropServices; using System.ComponentModel; sealed class Sound{ public static...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
1
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.