473,396 Members | 2,010 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,396 software developers and data experts.

strang function pointer from faq book (help)

I am a beginner in C++. I recently downloaded the codes of C++ faq
book.Since I don't have the book ,following is two program which is
confusing me a lot.

program 38_01.cpp
^^^^^^^^^^^^^^^^^
#include <iostream>
using namespace std;

class Fred {
public:
void f(int i) throw();
void g(int i) throw();
void h(int i) throw();
};

void Fred::f(int i) throw()
{
cout << "Fred::f(int); i=" << i << '\n';
}
void Fred::g(int i) throw()
{
cout << "Fred::g(int); i=" << i << '\n';
}
void Fred::h(int i) throw()
{
cout << "Fred::h(int); i=" << i << '\n';
}

typedef void(Fred::*FredMemberPt)(int);

void sample(Fred& x, FredMemberPt p) throw()
{
(x.*p)(42);
} // If p is &Fred::g, this is the same as x.g(42)

int main()
{
FredMemberPt p = &Fred::g;
Fred x;
sample(x, p);
}

I am having doubt with the line

typedef void(Fred::*FredMemberPt)(int);

In the above line I can able to understand that it is a declaration of
pointer to a function which is typedef ed. I have seen using the scope
resolution operator used to access the static variouble,member function
definition outside the class(and lot more,yet to learn). But the above
function name FredMemberPt does not exist in any of the code and yet it
has been compiled with out any errors. The same kind of deceleration is
in the next code also. I don't understand the above pointer to the
function declearation.I can understand the rest of the code but except
the declaration.
Can any body shed some lights?
program 38_04.cpp
^^^^^^^^^^^^^^^^^
using namespace std;
#include <iostream.h>
class Fred {
public:
void f(int i);
void g(int i);
void h(int i);
};

void Fred::f(int i)
{
cout << "Fred::f(int); i=" << i << '\n';
}
void Fred::g(int i)
{
cout << "Fred::g(int); i=" << i << '\n';
}
void Fred::h(int i)
{
cout << "Fred::h(int); i=" << i << '\n';
}

typedef void (Fred::*FredMemberPtr) (int);

FredMemberPtr array[3] = { &Fred::f, &Fred::g, &Fred::h };

int main()
{
Fred x;
for (int i = 0; i < 3; ++i)
{
FredMemberPtr p = array[i];
(x.*p)(42 + i);
}
}
--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Jul 23 '05 #1
3 1398
to******************@rediffmail.com wrote:


I am having doubt with the line

typedef void(Fred::*FredMemberPt)(int);

In the above line I can able to understand that it is a declaration of
pointer to a function which is typedef ed. I have seen using the scope
resolution operator used to access the static variouble,member function
definition outside the class(and lot more,yet to learn). But the above
function name FredMemberPt does not exist in any of the code and yet it
has been compiled with out any errors.

It is just an ordinary typedef.
Generally the rule works like this: Just write it as an ordinary
declaration eg.

int MyJ;

Then preceed the whole thing with 'typedef' and the name you define,
'MyJ', magically turns into a new data type name which is an alias
for the whole. So if you do

typedef int MyJ;

then MyJ gets an alias for the whole data type, in this case for 'int'.

In light of that

typedef void(Fred::*FredMemberPt)(int);

says:
FredMemberPt is an alias name for a data type. This data type is a
pointer to a member function of Fred, and it takes an int as argument
and returns void.

It is used to get a more friendly writing a little bit down. To define
an actual function pointer one would need to write:

int main()
{
void (Fred::*gFnct)(int) = &Fred::g;
}

which defines gFnct to be a pointer to a member function of Fred. Instead of
that clumsy syntax, one introduces a typedef to make that whole thing
cleaner:

int main()
{
FredMemberPt gFnct = &Fred::g;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
[my Q and your answer sniped...]
says:
FredMemberPt is an alias name for a data type. This data type is a pointer to a member function of Fred, and it takes an int as argument
^^^^^^^^^^^^^^^^^^^^^^^ and returns void.

But I don't find any member function called FredMemberpt in the class
Fred.All my doubts starts there.Pls explain.

[sniped....]

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Jul 23 '05 #3
to******************@rediffmail.com wrote:

[my Q and your answer sniped...]
says:
FredMemberPt is an alias name for a data type. This data type is a

pointer to a member function of Fred, and it takes an int as

argument
^^^^^^^^^^^^^^^^^^^^^^^
and returns void.


But I don't find any member function called FredMemberpt in the class
Fred.All my doubts starts there.Pls explain.

As said: The typedef defines a 'data type name'.
I don't know why you constantly search for a member function
called 'FredMemberPt' in Fred. The compiler doesn't spend a
millisecond to search for one, because the typedef does a completely
different thing: It defines a name for a specific data type. Nothing
more, nothing less.
The data type is: Pointer to member function in Fred, which takes an
int and returns void.
And the name given to that data type is: FredMemberPt

It is like a shortcut for you, the programmer. Such that you don't
have to constantly have to type that beast all over again, but can
use a shorter (and more readable name for it).

Consider:
Your program has to work with arrays of 'long unsigned int' all over
the places. Eg.

int main()
{
long unsigned int a[20][40];

...
}

Now you get tired of typing this monster all the time. Thus you define
a typedef for it:

typedef long unsigned int UIntAr[20][40];

which makes UIntAr a second name for the data type 'long unsigned int [20][40]'.

Then you can write:

int main()
{
UIntAr a;

...
}

That's what typedef does. While in the above array example it isn't such a good
idea, the typedef is a very good idea in the case of function pointers.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
3
by: Stijn Oude Brunink | last post by:
Hello I'm using std:vector to hold some pointers vector<CBase*> x; In the debug version I've the following problem: After I've added a couple of pointers using the push_back function and I...
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
21
by: Stephen Biggs | last post by:
Given this code: void f(void){} int main(void){return (int)f+5;} Is there anything wrong with this in terms of the standards? Is this legal C code? One compiler I'm working with compiles this...
19
by: sabarish | last post by:
Hi friend, what is the use of function pointer in c language and where it is useful? tell with simple example...? plz help me.
5
by: Chris Van Extergem | last post by:
Hi all, I'm just trying some examples from a book I'm reading and I just keep getting these seg faults. Below is the smallest possible program that still exhibits this behaviour. The full...
18
by: Robbie Hatley | last post by:
A couple of days ago I dedecided to force myself to really learn exactly what "strtok" does, and how to use it. I figured I'd just look it up in some book and that would be that. I figured...
6
by: Caleb | last post by:
I have a class that has two member functions of the same name: class MyClass { public: void someFunction(int arg); void someFunction(int arg, char blah); }; I'm trying to get a...
29
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.