Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:21:12+00:00 2026-05-25T19:21:12+00:00

This is my last question . I try to improve my class Thread. Constructor

  • 0

This is my last question. I try to improve my class Thread.
Constructor receives pointer to the function, that must run in a new thread.

class Thread { 
    public:
     Thread(void (*p)()) {
        pf=p;
      }
     ~Thread () {}
    void StartThread() {
     hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc, NULL, 0, &threadID); 
    }
    private: 
     void (*pf)();
     HANDLE hThread; 
     unsigned threadID;
     static unsigned WINAPI ThreadProc(LPVOID lpParam) {
      (*pf)(); //error C2597 illegal reference to non-static member
      return 0; 
     }
    }; 

In ThreadProc I need to call TimerFunc.

void TimerFunc () {
    i++;
}

Example of usage this class:

Thread *timer; 
timer = new Thread(TimerFunc);  
timer->StartThread();

So it doesn’t work.
Please somebody tell me, if this class is foolish.
May be it is a bad idea to send pointer to func which is situated outside class??
Thank you.

Thanks very much for your advices!
Now it works!

class Thread { 
public:
    Thread(void (*p)()) {
        gg.pf=p;
    }
    ~Thread ();
    void StartThread() {
     hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc, this, 0, &threadID); 
    }
private: 
    struct mm {
        Thread *pThread; 
        void (*pf)();
    } gg;
    HANDLE hThread; 
    unsigned threadID; 
    static unsigned WINAPI ThreadProc(LPVOID lpParam) {
        mm hh; 
        hh.pThread=static_cast<Thread*> (lpParam);
        hh.pf=hh.pThread->gg.pf;
        hh.pf();
        return 0; 
    }
}; 

What do you think? Is it correct choice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:21:13+00:00Added an answer on May 25, 2026 at 7:21 pm

    As the outhers pointed out, the problem is that a static method cannot access non-static members. The usual way to do something like this is to pass the object as parameter to the thread start routine, e.g.:

    class Thread { 
    public:
        Thread(void (*p)()) {
            pf=p;
        }
        ~Thread () {}
        void StartThread() {
            // Pass this as argument to ThreadProc
            hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc, this, 0, &threadID); 
        }
    private: 
        void (*pf)();
        HANDLE hThread; 
        unsigned threadID;
        static unsigned WINAPI ThreadProc(LPVOID lpParam) {
            // Get the passed Thread object
            Thread *pThread = static_cast<Thread*> (lpParam);
            pThread->pf();
            return 0; 
        }
    }; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I'm going to try to make this more clear because my last question
I'm going to give this another try because my last question might have been
Ive made mis-leading topic in my last question, so i open this new question
Continuing from my last question C++ template class map I have implemented the function
My last question was somewhat narrower than this one. I am interested to know
This is a followup to my last question . I now have a byte[]
I guess this is a continuation of the last question I asked: bulk insert
Okay, this one is the reverse of the last question I struggled with... I
Hopefully the last NN question you'll get from me this weekend, but here goes
This is a question regarding the timezone specified in the Expires and Last-Modified HTTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.