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.

2 Answers 2

1

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;
};
Sign up to request clarification or add additional context in comments.

Comments

0

You can declare S locally inside Foo.

class Foo
{
    struct S
    {
        Foo *ptrToFoo;
    };

    S sname;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.