help in resolving issue: static member function of class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lakshmank85
    New Member
    • Oct 2007
    • 6

    help in resolving issue: static member function of class

    Hi,

    i have a c++ class,

    class XYZ
    {
    public:
    void Function1 ( MyStruct * myStruct );
    // member variables, etc
    }


    Note that, Function1 is non-static function. MyStruct is some typdef struct

    now consider the following typdef.

    typedef void MyFunction ( MyStruct * myStruct );

    i have another function, (not part of any class). It takes a function pointer as its argument.

    void Function2 ( MyFunction * pFn )
    {
    // body
    }


    Then, i go about creating an object of class XYZ, and pass the address of Function1 to it,

    XYZ *object1 = new XYZ;
    Function2 ( object1->Function1 );


    This throws an error, cannot convert parameter 1 from 'void ( MyStruct *)' to 'MyFunction (__cdecl *)'
    however, when i declareFunction 1 as static void Function1 ( MyStruct * ), the compiler compiles without any error.

    Why is it that only a static member function is passed off without error and the non-static shows error?

    I have tried type casting too,
    Function2 ( (MyFunction*) object1->Function1 ); no avail.

    I have also tried creating an object without the new operator, even then, no avail.

    please help
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Class member functions have an implied first argument that is the address of the object the member function is to use. That is, the this pointer is passed by the compiler implicitly.

    Since static member functions do not apply to any particular object, they don't have this implied first argunent.

    That's why static member functions work when passed by address and reguklar member functions do not.

    Comment

    • Lakshmank85
      New Member
      • Oct 2007
      • 6

      #3
      how do i go about doing the same thing, without making it as non static? i tried the following:
      class XYZ
      {
      public:
      void Function1 ( MyStruct * myStruct );
      void (*ptrFunction1) (Mystruct*);
      // member variables, etc
      }


      //Constructor:
      XYZ()
      {
      ptrFunction1 = &Function1; //error in this line
      }


      ERROR: '&' : illegal operation on bound member function expression

      so i tried

      //Constructor:
      XYZ ()
      {
      this->ptrFunction1 = this->Function1; //error in this line
      }



      ERROR: XYZ::Function1' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
      ERROR: '=' : cannot convert from 'void (__thiscall XYZ::* )(MyStruct *)' to 'void (__cdecl *)(My_Struct *)'

      When i use ampersand (&), and also without it, i get error.

      Help, please

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Again I say, XYZ::Function1 is a member function. It has two arguments.
        The first is an XYZ* supplied by the compiler and the second is a MyStruct*.
        You cannot assign the address of this function to a function pointer to a function
        that has one argument.

        Also, it is the compiler that supplies this implicit first argument. You cannot
        do this yourself.

        The usual method here is to use a static member function since it has no implied
        arguments.

        I do question why you need the function pointer in the first place. Are you saying
        that a particular XYZ object is to call XYZ::Function1 using some other XYZ object?
        If so, you replace the function pointer with an XYZ* (or XYZ&) and then call
        XYZ::Function1 using the pointer or reference.

        Comment

        Working...