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

Thread: Function Pointers

  1. #11
    Code Warrior Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N's Avatar
    Join Date
    Sep 2008
    Location
    Kosovo
    Age
    18
    Posts
    4,034

    Re: Function Pointers

    i like it marwex .. +rep

  2. #12
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,164
    Blog Entries
    2

    Re: Function Pointers

    I appreciate it, egzon Thanks

    Posted via CodeCall Mobile

  3. #13
    Newbie Talo is an unknown quantity at this point
    Join Date
    Jun 2009
    Location
    The Netherlands
    Posts
    4

    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 .

  4. #14
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,164
    Blog Entries
    2

    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

  5. #15
    Newbie satishkumar432 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    5

    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 11:06 AM. Reason: add code tags (the # button)

  6. #16
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,164
    Blog Entries
    2

    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.

  7. #17
    Newbie satishkumar432 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    5

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

  8. #18
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,164
    Blog Entries
    2

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

  9. #19
    Newbie satishkumar432 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    5

    Re: Function Pointers

    Thanks again....got it.

+ Reply to Thread
Page 2 of 2
FirstFirst 1 2

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. XML Result from PHP?
    By Ricardo-san in forum PHP Forum
    Replies: 14
    Last Post: 04-09-2009, 11:03 PM
  2. function pointers
    By porno.shome in forum Pascal/Delphi
    Replies: 5
    Last Post: 07-26-2008, 02:42 AM
  3. C++ - function pointers as a static data member.
    By G_Morgan in forum C and C++
    Replies: 3
    Last Post: 07-07-2008, 09:36 PM
  4. using Template function
    By Chinmoy in forum C Tutorials
    Replies: 4
    Last Post: 04-03-2008, 04:16 AM
  5. Derived Classes Can't Set Function Pointers
    By dargueta in forum C and C++
    Replies: 11
    Last Post: 11-05-2007, 09:04 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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