+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Function Pointers

  1. #11
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: Function Pointers

    i like it marwex .. +rep

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #12
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Function Pointers

    I appreciate it, egzon Thanks

    Posted via CodeCall Mobile

  4. #13
    Talo is offline Newbie
    Join Date
    Jun 2009
    Location
    The Netherlands
    Posts
    4
    Rep Power
    0

    Talking Re: Function Pointers

    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 .

  5. #14
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Function Pointers

    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

  6. #15
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Re: Function Pointers

    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.
    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;
    }
    Clarification in this regard is very much appreciated.
    Last edited by WingedPanther; 07-09-2009 at 09:06 AM. Reason: add code tags (the # button)

  7. #16
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Function Pointers

    Quote Originally Posted by satishkumar432 View Post
    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.

    #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;
    }

    Clarification in this regard is very much appreciated.
    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:

    Code:
    (b1.*fptr)();
    It wants to see

    Code:
    base b1;
        
    void(base::*fptr)(void);
    fptr = &base::func;
    (b1.*fptr)();
    Which actually works.

    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

  8. #17
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Re: Function Pointers

    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)();

  9. #18
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Function Pointers

    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

  10. #19
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Re: Function Pointers

    Thanks again....got it.

+ Reply to Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Function Pointers
    By breimer273 in forum C and C++
    Replies: 4
    Last Post: 03-16-2011, 09:30 AM
  2. C function pointers question
    By tate in forum C and C++
    Replies: 7
    Last Post: 07-06-2010, 12:44 AM
  3. Function Pointers and Inheritance
    By cod3b3ast in forum C and C++
    Replies: 3
    Last Post: 01-12-2010, 05:37 PM
  4. function pointers
    By porno.shome in forum Pascal and Delphi
    Replies: 5
    Last Post: 07-26-2008, 12:42 AM
  5. Derived Classes Can't Set Function Pointers
    By dargueta in forum C and C++
    Replies: 11
    Last Post: 11-05-2007, 07:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts