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

  • SEARCH
  • Home
  • 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 8749483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:42:08+00:00 2026-06-13T12:42:08+00:00

In class Foo, I need a class member that is a struct S that

  • 0

In class Foo, I need a class member that is a struct S that contains a pointer to a Foo object.

Foo.h

Struct S{
    ...
    Foo *ptrToFoo
}

class Foo{ 
    Foo();
    ~Foo();
    ...
    S sname;
};

Now this fails obviously because the definition of sname contains a pointer to an object it has not seen yet.

The solution I have is to make an object (in a header file) just for S, and then used forward declaration:

S.h

class Foo;
class S{
    Foo *ptrtoFoo;
    ...
}

Foo.h

// can use S as an object as a data member in Foo

This seems really sloppy to me. Is there a better way to do this? S does not “warrant” its own object because it does not have methods; a struct would have been fine.

Now, if you don’t care why and just want to help, skip the rest. If you are thinking “this seems like bad design to me!”, read on.

The reason I am doing this is that pThreads_create does not take non-static class member functions as paramaters. The common solution (As many stackoverflow posts) point out, is to create a static member function that calls the real function you want, and then pass the caller object, like so:

void *Foo::callerHelper(void *arg)
{
    //http://stackoverflow.com/questions/7820653/pthread-create-parameter-function-pointer
    //http://stackoverflow.com/questions/86046/best-way-to-start-a-thread-as-a-member-of-a-c-class
    Foo *ptr = (Foo *)(arg);
    ptr->RealFnctnToCall();
    return 0;
}

...
pthread_create(&threads[id], NULL, &Foo::callerHelper, this)

HOWEVER, I need to pass not only this, but also other arguments. My “S” object represents the paramater payload. It contains the pointer to this, plus the real paramaters I need for “RealFnctnToCall”

S package;
S.FooPtr = this;
S.intINeed = ...
... 
//recast S as (void *)

and then do

pthread_create(&threads[id], NULL, &Foo::callerHelper, S)
// in callerHelper, depackage S and get everything I want. 

I am happy with my solution for this part, as long as there is a less sloppy way for my real question above.

  • 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-06-13T12:42:09+00:00Added an answer on June 13, 2026 at 12:42 pm

    How about forward declaring Foo directly in your existing code? That’s the canonical C++ way of doing it.

    class Foo;
    Struct S{
        ...
        Foo *ptrToFoo;
    };
    
    class Foo{ 
        Foo();
        ~Foo();
        ...
        S sname;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class foo that contains a std::auto_ptr member that I would like to
When I need a pointer to member of class, I do as following struct
class Foo(bar: String) { import Foo.Bar def this() = this(Bar) // this line fails,
I need to do something like that class Foo define perform() puts 'hello' end
There is something that confuses me. Let there be a class member Foo::$bar ,
I have a class that contains a private typedef and several member functions: class
I need to make a class with a generic data member, that works something
class Foo(){ public List<string> SomeCollection; } I need to implement an event which can
I need to implement a class called Foo where I need to Get and
My WidgetDoer class depends on Foo , which is not injected. I need to

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.