Results 1 to 4 of 4

Thread: Pointer to non static member functions within the class

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Pointer to non static member functions within the class

    Hi,

    after spending a lot of time on the following problem, without any luck, I first wanted to ask if my problem is solvable:

    Qt Code:
    1. typedef void (*fp)(int, const char*);
    2.  
    3. class A
    4. {
    5. public:
    6. A()
    7. {
    8. fp x;
    9. x = &A::staticMemberFunc; // works
    10. x = &A::memberFunc; // this doesn't, but I want it to...
    11. // (translated) error message:
    12. // error C2440: '=': 'void (__thiscall A::* )(int,const char *)' could not be converted to 'fp'
    13. }
    14.  
    15. private:
    16. void memberFunc(int, const char*) {}
    17. static void staticMemberFunc(int, const char*) {}
    18. };
    19.  
    20.  
    21. int main(int argc, char* argv[])
    22. {
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

    If it is, some hints are highly appreciated.


    Thanks,

    Lykurg

    P.s.: The typedef is part of a library so I can't change it.


    Added after 21 minutes:


    Damn! Not again Found the solution right after posting...

    Qt Code:
    1. class A
    2. {
    3. public:
    4. A()
    5. {
    6. foo = &A::memberFunc;
    7. fp x;
    8. x = (fp) &foo;
    9. }
    10.  
    11. void (A::*foo)(int, const char*);
    12. void memberFunc(int, const char*) {}
    13. };
    To copy to clipboard, switch view to plain text mode 


    Added after 18 minutes:


    Ehm, , no compiler error but I get a crash when using x.
    Last edited by Lykurg; 23rd April 2011 at 17:15.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pointer to non static member functions within the class

    Ehm, , no compiler error but I get a crash when using x.
    As long as you don't touch the parameters, it should work (but will be quite useless). First parameter of a class member method is an address of calling object ('this'), so casting it like that will cause a mess, look at this one:
    Qt Code:
    1. #include <iostream>
    2.  
    3. typedef void (*fp)(int, int);
    4.  
    5. class A
    6. {
    7. public:
    8. void method( int x, int y){
    9. std::cout << (int)this << " " << x;
    10. }
    11. };
    12.  
    13. typedef void (A::*fpa)(int,int);
    14.  
    15. int main(int argc, char* argv[])
    16. {
    17. fpa x = &A::method;
    18.  
    19. fp f;
    20. f = (fp)(x);
    21. f(44,55);
    22.  
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 
    output is : 44 55
    Arguments got "shifted", because an address of calling object is expected as first parameter ( so in fact it is void method( A * this, int, int ) ). I think it's clear enough that any use of 'this' (referencing) in such method will cause a crash, as 'this' is not valid address of calling object ( trying to call object's method without an object ). Last parameter passed will probably contain some garbage values.

    Is this some kind of callback mechanism and you want to register object's method ?
    Last edited by stampede; 24th April 2011 at 00:41.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Pointer to non static member functions within the class

    Hi,

    thanks for your comment, I see a little bit clearer now. But also your example don't compile (msvc) or crashes with mingw.

    My original "problem" is to make a library call to astyle (because I had rearrange my plugin last time and want to release a new version). The idea was to encapsulate all astyle stuff in a single class, with simple returns the formatted source code and it should be used in parallel to speed up things for future. I can make a work around with a static error function and a mutex and grab the error string inside formatedText(), but I it would be nicer to directly use a member function (if possible).

    Qt Code:
    1. QString ASInstance::formatedText(const QString& text)
    2. {
    3. m_error = false;
    4. QByteArray baIn = text.toUtf8();
    5. const char* textOut = AStyleMain(baIn.constData(),
    6. m_options.constData(),
    7. /* MAGIC goes here, must be fpError */,
    8. &ArtisticStyle::Internal::ASInstance::memoryAlloc);
    9. QString formattedText = QString::fromUtf8(textOut);
    10. delete [] textOut;
    11. return formattedText;
    12. }
    13.  
    14. void ASInstance::errorHandler(int number, const char* message)
    15. {
    16. m_error = true;
    17. m_errorNumber = number;
    18. m_errorMessage = QString(message);
    19. }
    To copy to clipboard, switch view to plain text mode 

    and from astyle.h
    Qt Code:
    1. typedef void (STDCALL* fpError)(int, const char*); // pointer to callback error handler
    2. extern "C" EXPORT char* STDCALL AStyleMain(const char*, const char*, fpError, fpAlloc);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pointer to non static member functions within the class

    But also your example don't compile (msvc) or crashes with mingw.
    I don't have msvc compiler, but it worked with g++ 4.5.2, nevermind anyway.
    If you want to use member method, the only thing I can think of in that case is to pass static class method pointer as callback and use member method of static instance inside, but it has obvious limitations. I think it's not possible to use member method directly, because of the "this" issue.
    Maybe someone else has encountered that kind of problem and can help you more.

Similar Threads

  1. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 14:53
  2. qt problem with tr() in static member of a class
    By Krzysztow in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2010, 11:29
  3. Using a static member in a class
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 29th April 2010, 10:58
  4. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  5. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.