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 6715201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:35:11+00:00 2026-05-26T08:35:11+00:00

i have a Base class and Derived class. they have a virtual function- virtual

  • 0

i have a Base class and Derived class.
they have a virtual function- virtual void action()
how can i pass it to *pthread_create()* function?

example(with errors):

class Base{
  protected:
     pthread_t tid;
  public:
  virtual void* action() = 0;
};

class Derived : public Base{
  void* action();
  Derived(){
    pthread_create(&tid, NULL, &action, NULL);
  } 
};

maybe it should be static?
i tried lot of combinations but cant find solution..

  • 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-26T08:35:11+00:00Added an answer on May 26, 2026 at 8:35 am

    I ran into this problem a couple months back working on my senior design project. It requires some knowledge of underlying C++ mechanics.

    The underlying issue is that pointers to functions are different from pointers to member functions. This is because member functions have an implicit first parameter, this.

    From the man page:

    int pthread_create(pthread_t *thread,
                       const pthread_attr_t *attr,
                       void *(*start_routine) (void *),
                       void *arg);
    

    The thread entrance point is a void* (*)(void*). Your function, Base::action has the type void* (Base::*)(). The Base:: part of that ugly type declaration denotes the type of this. The type discrepancy is why the compiler won’t accept your code.

    There’s two things we need to fix to make this work. We can’t use a member function, because pointers to member functions don’t bind this to an instance. We also need a single parameter of type void*. Thankfully, these two fixes go hand in hand because the solution is to explicitly pass this ourselves.

    class Base {
    public:
        virtual void* action() = 0;
    
    protected:
        pthread_t tid;
    
        friend void* do_action(void* arg) {
            return static_cast<Base*>(arg)->action();
        }
    };
    
    class Derived : public Base {
    public:
        Derived() {
            // This should be moved out of the constructor because this
            // may (will?) be accessed before the constructor has finished.
            // Because action is virtual, you can move this to a new member
            // function of Base. This also means tid can be private.
            pthread_create(&tid, NULL, &do_action, this);
        }
    
        virtual void* action();
    };
    

    Edit: Woops, if tid is protected or private, then do_action needs to be a friend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class hierarchy in C++: class Base { virtual void apply()
I have a simple C++ base class, derived class example. // Base.hpp #pragma once
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have a base class A and a derived class B: class A {
Suppose I have a base class B, and a derived class D. I wish
I have an abstract base class and derived class: type TInterfaceMethod = class public
I have a tempated base class check and publically derived class childcheck. the base
I have a c++ class derived from a base class in a framework. The
I have a base class which is non-generic with a derived generic class. The
Let's say I have a Base class and several Derived classes. Is there any

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.