Question

Why I can't have pointer to member like this:

#include <iostream>
#include <stdlib.h>     /* malloc, free, rand */
using namespace std;

class Pool{};

struct FE{
    static Pool pool;
};

Pool FE::pool;

int main() {
    Pool FE::* pmd = &FE::pool;
    return 0;
}

http://ideone.com/UabI61

What I am doing wrong?

Was it helpful?

Solution 2

pool is a global variable that happens to live in the scope of FE; pointers to members only point to members of instances of classes.

OTHER TIPS

Pointer-to-members are essentially offsets into an object. That is, if you have an FE object a pointer-to-member indicates a constant offset from the address of each FE object. A static object won't have a constant offset from each FE object. Thus, the type of &FE::pool is Pool* and not Pool FE::*. The two pointer types are incompatible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top