Thread: Address of a non-static class member function?

  1. #1
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    Address of a non-static class member function?

    Is there any way to get the address of a non-static class method ?

    Something like pSomeProcPtr= &CSomeClass::someFunction() (which unfortunatly does not compile) ?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The dilemma here is that a pointer needs an address, but there is no “address” inside a class; selecting a member of a class means offsetting into that class. You can’t produce an actual address until you combine that offset with the starting address of a particular object. ("Thinking in C++")
    Code:
    #include <iostream>
    using namespace std;
    
    class foo
    {
    public:
    	foo(int num)
    	{
    		number = num;
    	}
    	
    	int add(double n)
    	{
    		cout<<"In function: add()"<<endl;
    		return number + n;
    	}
    private:
    	int number;
    };
    
    int main()
    {
    	int (foo::*pfunc)(double) = &foo::add;
    	
    	foo f(10);
    
    	(f.*pfunc)(3.0);
    
    	return 0;
    }
    On VC++ 6.0, the address operator is optional.
    Last edited by 7stud; 08-23-2003 at 05:08 PM.

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    Hope this will help you out a bit....i don't think it will but maybe.
    I am doing something similar to this (I think). I am a noob so tell me if this is what your trying to do.
    Code:
    void Test::print() const
    {
       //implicitly use this pointer to access member x
       cout <<"             x= " <<x;
    
       // explicitly use this pointer to access member x
       cout <<"\n this->x= " << this->x;
    
       // explicitly use dereferenced this pointer and the dot operator
       // to access member x
       cout <<"\n(*this).x = " <<( *this ).x <<endl;
    }
    Hope this helps.
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    RealityFusion: Unfortunatly your code won't help me in this particular case, but thank you for trying!

    7stud, thank you, that seems to work and is pretty much exactly what I wanted. I think =)

    btw... nothing special, but I got a compiler error and had to use return number + int(n);.

    Thanks again!

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Ok, now I have another problem. This code...

    Code:
    int (foo::*pfunc)(double) = &foo::add;
    ... works, but is there some way to make a type out of it. Right now I don't even know what it is. Where is the type? pfunc is the "variable" as far as I understand it.

    Is it at all possible to typedef it and so I can use it like...

    Code:
    PCfMyType pofMyVar;
    pofMyVar= &foo::add;
    ?
    Last edited by darksaidin; 08-25-2003 at 05:02 AM.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    Try this:

    Code:
    typedef int (foo::*PCfMyType)(double);
    
    PCfMyType pofMyVar;
    pofMyVar= &foo::add;

  7. #7
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Thank you, that works and saves me from countless callbacks to functions outside of classes or even worse, static classmembers.

    A happy DarkSaidin
    [code]

    your code here....

    [/code]

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    int (foo::*pfunc)(double) = &foo::add;

    The variable name is pfunc, and the type is:

    int (foo::*) (double)

    which can be read: a pointer to a foo member function which has a parameter of type double and a return type of int.

  9. #9
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Sorry to bump this post again, but there are two more things I'd like to know, if possible.

    Is there any "normal" way to typecast such a pointer to a void pointer ?
    At the moment I do this with memcpy, but that's ... ugly style.


    Second question... ... is going to be a bit longer: I have created a baseclass with a protected function registerHandler(string sName, [...], PCfVoid pofVoidFunc); that allows derived classes to register their new functions. (I've three registerHandler functions for different parameterlists, but that doesn't matter, I think).

    The addresses will be stored in some kind of array maintained by the baseclass. (Thats also why I would like to have them as (void*)'s).

    A "motherclass" has friend access to this array. That way it can access functions of an unknown class without having to use predefined abstract functions in the baseclass.

    However, PCfVoid is a pointer to a function in the baseclass, so I need to typecast the addresses of functions from the derived class when registering them like registerHandler("MOVEUP", [...], (PCfVoid)&CDemoMacroHandler::macroMOVEUP);

    So to finally come to my question: I've not done all of the implementation yet so I don't know if all this works at all. Does anyone of the C++ ... veterans(?)... see any problems here ? i.e. I'm a bit afraid that the addresses might somehow be invalid for the derived class and would only be valid in the baseclass, if it had those functions. (Would be cool to know before I type all the code only to realize it's "impossible" )


    Anyway, thanks for all the help and explanations so far!


    edit: nevermind It works.
    Last edited by darksaidin; 08-25-2003 at 05:56 PM.
    [code]

    your code here....

    [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Static member of a class
    By Gravedigga in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2005, 03:54 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. Is it possible to have callback function as a class member?
    By Aidman in forum Windows Programming
    Replies: 11
    Last Post: 08-01-2003, 11:45 AM