member member function pointers

This is a discussion on member member function pointers within the C++ Programming forums, part of the General Programming Boards category; im trying to create a member function pointer, but the problem is that i need to make that pointer a ...

  1. #1
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38

    Question member member function pointers

    im trying to create a member function pointer, but the problem is that i need to make that pointer a member of the class as well. heres a sample of what im trying to:

    Code:
    //gfxclass.h
    
    class gfxclass
    {
    public:
        //this is my member function pointer
        int (gfxclass::*RGB)(int, int, int);
    
        int initgfx(void);
    private:
        //here are the two functions it could point to
        int gfx1(int r, int g, int b);
        int gfx2(int r, int g, int b);
    };
    Code:
    //gfxclass.cpp
    
    #include "gfxclass.h"
    
    int gfxclass::init(void)
    {
        if(whatever)
            RGB = gfx1;
        else
            RGB = gfx2;
    }
    
    int gfxclass::gfx1(int r, int g, int b)
    {
    //whatever, do stuff
    }
    
    int gfxclass::gfx2(int r, int g, int b)
    {
    //whatever
    }
    Code:
    //main.cpp
    
    #include "gfxclass.h"
    
    gfxclass gclass;
    
    int main()
    {
        gclass.init();
    
        int temp = (gclass.*RGB)(0,0,0);
    
        return 0;
    }
    when i go to compile, i get an undeclared identifier error on my call to RGB. ive managed to get it to compile with the member function pointer not being part of the class, but i need it to be a member. any help would be appreciated
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Using a member function pointer to execute a member function has a wacked syntax. By the way, I've never been able to get the syntax in those tutorials to work(VC++6).

    The member function pointer is accessed using an object of the class: gclass.RGB. To get the function, you need to dereference that, which gives you: *gclass.RGB. Now, you need to call that function with a class object:

    (gclass.*gclass.RGB)(0, 0, 0);

    The parentheses matter. Since *gclass.RGB is the actual member function, you can even create a second object, say gclass2, to call the member function represented by *gclass.RGB:

    (gclass2.*gclass.RGB)(0,0,0);
    Last edited by 7stud; 12-04-2005 at 04:20 PM.

  4. #4
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    tyvm for your help! that problems been bugging me for a while now, but it finally compiled properly
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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