Like Tree1Likes
  • 1 Post By manasij7479

Pointer to member function

This is a discussion on Pointer to member function within the C++ Programming forums, part of the General Programming Boards category; Here is the program which uses pointers to members, variables and functions. Pointer to variables are done without error as ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    Pointer to member function

    Here is the program which uses pointers to members, variables and functions.
    Pointer to variables are done without error as i checked keeping the blocks with problem in comment line.
    The problem is with member function. I did it as it was given in book and even saw same format on net.
    The errors are like this:cannot call member function 'void' sejal::get without object.
    The compiler is Bloodshed's Dev c++




    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<iostream>
    using namespace std;
    class sejal
    {
          public:
          int y;
                 void get();
                 void put();
                 friend sejal add(sejal,sejal);
                 };
                 void sejal::get()
                 {
                      cout<<"\nEnter the value";
                      cin>>y;
                      };
                      void sejal::put()
                      {
                           cout<<"\nThe value is :"<<y;
                           };
                          sejal add(sejal m, sejal k)
                           {
                                     sejal o;
                                     o.y=m.y+k.y;
                                     cout<<"The addition value is:";
                                     return(o);
                                     };
                                     main()
                                     {
                                           sejal a,b,c;
                                           a.get();
                                           b.get();
                                           c=add(a,b);
                                           c.put();
                                           /*using pointer to member variable*/
                                           int sejal::*p=&sejal::y;
                                           cout<<"\nPrinting using pointer to member"<<":"<<a.*p<<"\n"<<b.*p;
                                           cout<<"Now taking pointer to object";
                                           sejal *i;
                                           i=&a;
                                           sejal *h;
                                           h=&b;
                                           cout<<"\n"<<"Printing using pointer to object"<<i->*p<<"\n"<<h->*p;
                                           cout<<"Using pointers to member function";
                                           void (sejal::*pf)(void)=&sejal::get();
                                           a.*pf();
                                           b.*pf();
                                           void (sejal::*pg)(void)=&sejal::put();
                                           a.*pg();
                                           b.*pg();
                                           cout<<"\nNow using pointer to object\n";
                                           i->*pf();
                                           h->*pf();
                                           cout<<"\nPrinting";
                                           i->*pg;
                                           h->*pg;
                                           cout<<"Done";
                                           getch();
                                           }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    1. main() must return an integer or your program will not compile on a modern compiler.

    2. You've seriously misunderstood(If you've written this yourself) almost everything regarding pointers to members and pointers to objects.
    Read [33] Pointers to member functions ..Updated!.., C++ FAQ

    3. Your indentation is awful; you're writing code, not making a staircase.
    Last edited by manasij7479; 09-02-2011 at 07:51 PM.
    Salem likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'll have to disagree with the honorable manasij on point 2; the only thing wrong I see with the syntax is that this
    Code:
    a.*pf();
    needs to be this
    Code:
    (a.*pf)();
    (and of course ditto on the following lines). (EDIT: Oh, and the last set of pg calls are missing their last () as well, which seem to have migrated to the get and put on the lines defining pf and pg.)

    We agree wholeheartedly on point 3, though.
    Last edited by tabstop; 09-02-2011 at 10:50 PM.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by tabstop View Post
    I'll have to disagree with (:P) manasij on point 2; the only thing wrong I see with the syntax is that this.
    What he basically is doing:
    Code:
    class X
    {
    public:
        void foo();
    };
    void (*bar)();
    int main()
    {
        bar = X::foo;
        return 0;
    }
    which apparently is invalid.

    Please correct me if I'm wrong.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Imagine you have to write a 1000 lines file... do you tilt your screen to read your code?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    manasij is right, you can't assign a non-static member function to a function pointer. The code manasij posted would work if the member function foo() was made static (i.e., "static void foo();").

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    >>void (sejal::*pf)(void)=&sejal::get();
    This should be (at the very least)
    void (sejal::*pf)(void)=&sejal::get;

    Also, in manasij7479's example, bar = X::foo; is wrong.
    It should be bar = &X::foo;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Also, in manasij7479's example, bar = X::foo; is wrong.
    It should be bar = &X::foo;
    That is also wrong.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Let's just put out correct code on calling member functions to avoid confusion...

    Code:
    class X
    {
    public:
        void foo();
    };
    
    typedef void (X::*X_foo_t)();
    
    int main()
    {
        X_foo_t bar = &X::foo;
        X x;
        (x.*bar)();
        return 0;
    }
    I think I may be half to blame for not entirely reading the thread correctly...
    Last edited by Elysia; 09-03-2011 at 11:29 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Elysia View Post
    Let's just put out correct code on calling member functions to avoid confusion...

    Code:
    class X
    {
    public:
        void foo();
    };
    
    typedef void (X_foo_t)();
    
    int main()
    {
        X_foo_t* bar = X::foo;
        X x;
        (x.*bar)();
        return 0;
    }
    I think I may be half to blame for not entirely reading the thread correctly...
    First you correct manasij7479 for not using & to get the address of foo, and then you do the same thing yourself
    Code:
    typedef void (X::*X_foo_t)();
    X_foo_t bar = &X::foo;

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Yeah, I'm getting forgetful and not compiling code.
    But thankfully there's people like you who can correct me!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Member Function Pointer...
    By yaya in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2009, 04:37 PM
  2. Pointer to member function?
    By C+/- in forum C++ Programming
    Replies: 9
    Last Post: 05-13-2007, 07:58 AM
  3. Pointer to member function
    By pheres in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2007, 05:18 AM
  4. Replies: 3
    Last Post: 07-23-2006, 01:09 PM
  5. Pointer to a Member Function
    By TheDan in forum C++ Programming
    Replies: 25
    Last Post: 04-03-2006, 08:18 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21