简体   繁体   中英

Declare a struct defined in class

There is a header file:

class btCollisionWorld
{
public:

    struct RayResultCallback{
        int something; //example only
    };
)

I'm writing another headeer file, where I want to use pointer to btCollisionWorld ::RayResultCallback , but I don't want to include whole btCollisionWorld.h (I will include it in my cpp file)

How do I declare it properly?

I've tried this and it fails:

class btCollisionWorld;
struct  btCollisionWorld::ClosestRayResultCallback;

You can't declare a struct defined inside a class without defining the containing class. You can use a namespace to achieve a similar goal.

It's only reasonable to keep this other file seperated from the btCollisionWorld.h if it does not really rely on the specific class btCollisionWorld , but rather just on some class with certain properties which btCollisionWorld fulfills. In this case, it might be better to keep it generic, that is: rather than using a btCollisionWorld* you may do

template <typename btCollisionWorldT>
whatever-kind-of-structure-it-is {
  btCollisionWorldT * genericpointer;
}

If the structure is a class, you can later typedef it so that btCollisionWorldT is btCollisionWorld in every actual instance of this class.

Alternatively you can use a void* , but that is less likely to be the ideal solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM