简体   繁体   中英

Limit using/typedef to class scope

I need to store a function prototype inside of a class somehow, but it automatically becomes a global when I try to do so.

The prototype is given trough a template, but I cannot use it straight out of there as I use it inside of thread member function which has to be static (Because otherwise, I break the callback prototype because of the this pointer).

I cannot have it go global, as I would need to run multiple instances of the class and the prototypes would get jumbled up.

Is there a way to do this?

template<class proto>
class cl
{
private:
    using m_proto = proto;
    void* addr;
public:
    static void thread(void* p)
    {
        // p receives a pointer to the current class
        cl* clp = (cl*)p;
        ((m_proto)clp->addr)();
    }
};

You can use

void (*addr)();

This declares addr as a member variable of type void (*)() .

You can make it more readable by using.

using my_ptr_type = void (*)();
my_ptr_type addr;

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