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.

Mark B
  • 95,107
  • 10
  • 109
  • 188
Tommy
  • 12,588
  • 14
  • 59
  • 110

2 Answers2

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;
};
Mark B
  • 95,107
  • 10
  • 109
  • 188
0

You can declare S locally inside Foo.

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

    S sname;
};
avakar
  • 32,009
  • 9
  • 68
  • 103