473,222 Members | 1,833 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,222 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 1391
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.