When C + + calls the Pthread_create function, the members in the incoming class error. Workaround.

Source: Internet
Author: User

Tag: ATI return match requires static member color object-oriented names span

1. Problem arises

I want to translate my C program into C + +, because I think C + + 's object-oriented approach is particularly useful.

And then I started porting, and when I wanted to call pthread_create in a class function to create a class.

The parameters I gave are

S=pthread_create (&id,null,run,null); // function Prototypes void * Thread_433::run (void *arg) {    void * ret;     using namespace   std;    cout<<"hello!\r\n";     return ret;}

This time it will compile error,

D:\Cprogress\pthread\my_thread.cpp:'thread_433::run' from'  void* (thread_433::) (void*)"void* (*) (void*)'     s =pthread_create (&id,null,run,null);                                        ^

He says this type does not match.

2. Problem solving

There is a type mismatch problem. Because pthread_create requires a parameter type of void* (*) (void*), and run acts as a member function of the class, its type is a member function pointer of void* (thread_433::) (void*).

We know that the member functions of a class will become a global function with the this pointer parameter after being processed by the compiler, so the type is doomed to be mismatched.

However, if you declare run as a static type, the compiler converts the static form of a function into a global function without the this pointer, so its type can match the type of parameter that Pthread_create requires. However, a static member function of a class cannot access a non-static member of a class, but this can be resolved by passing the this pointer.

Modified code: No other need to change

voidThread_433::init (void){    ints; S=pthread_create (&id,null,thread_433::run,null); if(s!=0) Std::cout<<"err!\r\n";}/********** class declaration ***********/classthread_433{ Public: thread_433 (); voidInit (void); voidDelete (void);Private: pthread_t ID; Static void*run (void*arg);};

When C + + calls the Pthread_create function, the members in the incoming class error. Workaround.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.