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

strang function pointer from faq book (help)

P: n/a
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
Share this Question
Share on Google+
3 Replies


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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.