* Andrew Falanga:
> On Mar 6, 9:27 am, "Alf P. Steinbach" <al...@[EMAIL PROTECTED]
> wrote:
>> * Andrew Falanga:
>>
>>
>>
>>> About two weeks ago (or so) I posted here that I was having problems
>>> with getting a member function pointer to point to a member function
>>> without giving me grief.
>> Yes, as a novice you should not use member functions /at all/.
>>
>>
>>
>>> I've gone to trying this on a trial program
>>> that I will post below in its entirety. I just can't get it right and
>>> although I've tried the suggestions given to me from the other
>>> posting, I'm still doing something wrong. So, here's my test program:
>>> #include <iostream>
>>> class base {
>>> int somedata;
>>> public:
>>> base();
>>> int GetData() {
>>> return somedata;
>>> }
>>> int (base::*ptrMemFunc)();
>>> };
>>> base::base() : somedata(5), ptrMemFunc(&base::GetData)
>>> {}
>>> // this one's only in here to make sure I remembered how to use
>>> function pointers
>>> int AnInt() {
>>> return 5;
>>> }
>>> int main( ) {
>>> base b;
>>> int (*pAnInt)() = AnInt;
>>> std::cout << pAnInt() << std::endl;
>>> b.ptrMemFunc();
>> "b.ptrMemFunc" is an expression that refers to a non-static member
function pointer.
>>
>> "()" tries to call the referred non-static member function, without
specifying
>> an object.
>>
>
> Doesn't the "b." reference an object?
It does. But it's unrelated to the call. The call "()" operates on the
entire
expression.
See DES^3 posting else-thread for appropriate syntax.
But don't use it. Use virtual member functions instead. Be advised that
member
function pointers are at the level of "goto": they allow you to do very
risky
things, and the compiler won't complain (it is the C++ principle of
letting the
programmer do whatever's needed, even if it doesn't look good to the
compiler).
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


|