i like it marwex.. +rep
![]()
I appreciate it, egzonThanks
Posted via CodeCall Mobile
You explained it great, but I still can't get my head around it to be honest. Well everybody says it's a difficult subject i'll re-read it later today and try again.
Thanks for making this tutorial marwex89.
Thanks, Talo (and Welcome to CodeCall)... Have a coffee and re-read it, make some small example programs, and you'll soon get the hang of it
![]()
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Hello Marwex89,
Thnx for posting very useful document on function pointers. This post is pretty much helpful to novice programmers like me in C++ to understand these fuzzy concepts. But I still have a small doubt regarding it.
Please refer to the below code. In the below code Im trying to access the function pointer initialized in the constructor, but the first statement((b1.*fptr)()always throws a compilation error, but the with the statement (b1.*(b1.fptr))()) the code works as expected.
Clarification in this regard is very much appreciated.Code:#include<iostream> using namespace std; class base { public: void(base::*fptr)(void); void func(void) { cout<<"Hello im in func"<<endl; } base() { fptr=&base::func; } }; int main() { base b1; (b1.*fptr)();//Compilation Error as "fptr undelcared" (b1.*(b1.fptr))()) //works perfectly fine. return 0; }
Last edited by WingedPanther; 07-09-2009 at 09:06 AM. Reason: add code tags (the # button)
Note: Use the code tags (# button) when posting code![]()
This makes sense because the compiler thinks fptr is a variable when you call it this way:
It wants to seeCode:(b1.*fptr)();
Which actually works.Code:base b1; void(base::*fptr)(void); fptr = &base::func; (b1.*fptr)();
While when you type *(b1.fptr) you dereference the value that b1.fptr returns, i.e. your member function pointer.
Welcome to CC and feel free to ask if you have more questions.![]()
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Thank you marwex89 for a quick reply ....But still a doubt creeps in my mind please bear me...
.
base b1;
void(base::*fptr)(void);
fptr = &base::func; ..........Why do we need to initialize it again when we have already done it in the constructor.
(b1.*fptr)();
Ah, you didn't quite get the point... This fptr is NOT the same as your class member! It is a separate local variable in your main function. You could rename it to e.g. "my_variable" and it would still work. For using the member variable you can do (as you wrote)
Code:(b1.*(b1.fptr))();
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Thanks again....got it.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks