CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2006
    Posts
    6

    Calling static member function from non-class function

    I'm having trouble calling a static class member function (that returns a pointer to a singleton) from a non-class function. When I try to compile (with mingw gcc) I get an error. The problem code is below:
    Code:
     void executer(void* params)
     {
        FunctionParameters* args = static_cast<FunctionParameters*>(params);
    
        std::string name = (*args)[0];
    
        TFunctor* func = IConsole::interface()->getCommand(name);
    
        if(func != 0)
            func->Call(args);
        else
            IConsole::interface()->add("Function Not Found");
     }
    The code works well from inside the IConsole class but but i need to pull it out so that i can eventually run it as a pthread. If I pass the pointer to IConsole to the function like so:
    Code:
    void executer(void* params, IConsole* con)
    {
        FunctionParameters* args = static_cast<FunctionParameters*>(params);
    
        std::string name = (*args)[0];
    
        TFunctor* func = con->getCommand(name);
    
        if(func != 0)
            func->Call(args);
        else
            con->add("Function Not Found");
    }
    everything works perfectly, except i can't use this to start a pthread. The IConsole->interface() call is used excessively throughout the many classes in the program without any trouble but always from inside a class. Is there some trick to calling the static class member function from a non-member class.

    Any Ideas greatly appreciated.

  2. #2
    Join Date
    Oct 2006
    Posts
    216

    Re: Calling static member function from non-class function

    What compiler errors and in which lines are you getting?

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Calling static member function from non-class function

    is IConsole::interface a public function?

    Please give full details of your problem, including the text of the compiler error and the line of code that it objects to. A small, compilable sample that generates the error would be best of all.

    As I told someone else, I've had to turn my telepathy off recently, because it was using too much energy.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Jun 2008
    Location
    Kuala Lumpur, Malaysia
    Posts
    68

    Re: Calling static member function from non-class function

    I'm not sure whether I understand your problem correctly.

    Quote Originally Posted by djwhy
    everything works perfectly, except i can't use this to start a pthread.
    What does it mean? Is it because the function has 2 parameters, thus its signature is different than the one which library function expects? If that's the case, you could make a structure
    Code:
    struct SConsoleAndParam
    {
        void*		params;
        IConsole*	con;
    };
    
    void executer(void* pConsoleAndParam)
    {
        void* params = reinterpret_cast<SConsoleAndParam*>(pConsoleAndParam)->params;
        IConsole* con = reinterpret_cast<SConsoleAndParam*>(pConsoleAndParam)->con;
    
        FunctionParameters* args = static_cast<FunctionParameters*>(params);
    
        std::string name = (*args)[0];
    
        TFunctor* func = con->getCommand(name);
    
        if(func != 0)
            func->Call(args);
        else
            con->add("Function Not Found");
    
        free( pConsoleAndParam ); // assume structure is allocated on heap
    }
    You can allocate the structure on the heap before creating thread, set values to its members, and pass its address.

  5. #5
    Join Date
    Mar 2006
    Posts
    6

    Re: Calling static member function from non-class function

    Thanks for the replies so far. Sorry i didn't post the error message I just couldn't stand looking at it any more. The error is:
    Code:
    expected unqualified-id before "struct"
    And it appears anywhere that I use IConsole::interface() within the function.
    As I understand it this means there is something wrong with the way I'm using the IConsole class.

    I Just tried to put together some a code that would illustrate my point but it compiled so there must be something else wrong. Perhaps the way I'm using the singleton template or the order I'm defining everything.

    I think for now I'm leaning to something like muses idea and come back to it later.

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Calling static member function from non-class function

    I didn't see you call a static member function from non member function.

  7. #7
    Join Date
    May 2008
    Posts
    32

    Re: Calling static member function from non-class function

    (*args) gives you the value of the zeroth item of the array. In general
    *(args+i) gives you the (i+1)th element of the array. I don't know why you have written (*args)[0] . Try to fix this and then get back.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured