Function pointers with classes

This is a discussion on Function pointers with classes within the C++ Programming forums, part of the General Programming Boards category; I have a structure that looks like this: Code: struct STRUCTURE { bool (*ptFunction)(void); }; So it has a pointer ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Function pointers with classes

    I have a structure that looks like this:

    Code:
    struct STRUCTURE
    {
      bool (*ptFunction)(void);
    };
    So it has a pointer to a function in it (if that * is on the wrong side don't worry about it).

    I also have a class like this:
    Code:
    class CONNECTION
    {
      public:
        bool Assign();
        bool MyPtrFunction(void);
    };
    I then want to do this:
    Code:
    bool CONNECTION::Assign()
    {
      STRUCTURE myStruct;
      myStruct.ptFunction = MyPtrFunction;
    }
    But I can't do that because im assigning a
    bool (__stdcall CONNECTION::*) (void) to a
    bool (__stdcall *)(void).

    Is there any possible way to assign the CONNECTION::MyPtrFunction to ptFunction? Any help is much appreciated!

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I think CONNECTION::MyPtrFunction must be static.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks jlou, that seems to have removed the errors from assigning the functions. But now I get this:

    error C2228: left of '.length' must have class/struct/union type

    for the line:
    Code:
    if (*lpdwSize < PostData.length())
    Where PostData is a string that belongs to the CONNECTION class. What does it mean? I changed the declaration for the functions in the class to
    static bool MyPtrFunction(void)

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    If the function is static, you can't access any non-static members or methods of the class, since there is no "instance" of the class to use.

    It sounds like the static thing is not what you wanted, since you want to actually be able to run the function in the context of its instance. If that's true, then I can't really help you. I was ablle to get your initial example to build without making the CONNECTION::MyPtrFunction static, (I declared the function pointer in STRUCTURE as bool (CONNECTION::*ptFunction)(void);) but then I couldn't call the function from myStruct. Maybe it is possible but I personally don't know how.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeh unfortunatley I can't do that, the STRUCTURE struct is something I can't actually change (its actually an EXTENSION_CRONTROL_BLOCK). I guess I'll have to make the functions global functions.... unless anyone knows how I can do this?

  6. #6
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    If your trying to get a function pointer to a member function of an object, you cant do it directly. I asked about this several months back, and the end result is code something like this (i removed my specific code and just left the concept):

    Code:
    class Object {
    	static void staticmemberfunction(void *param) 
    	{
    		Object *thisObject = static_cast<Object*>(param);
    		thisObject->nonstaticfunction();
    	}
    	void nonstaticfunction(void);
    };
    Your basically using a static member function to access a nonstatic one, so instead of making a global function, this would be easier. You determine which instance of the object gets the non-static function call by passing it the "this" pointer from inside itself, or a pointer to it from outside. This way it can call the function in itself, or another instances of Object. Btw if i screwed up some part of this explaniation, or there is a better way, im sorry, im looking at code from months ago doing this from memory. Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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