T
tomailmelookatbottom
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;
(x.*p)(42 + i);
}
}
--
"combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
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;
(x.*p)(42 + i);
}
}
--
"combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com