Pointer to member of derived type

closed account (z0My6Up4)
My compiler throws an error cannot convert a pointer to a member of a derived type to a pointer to a member of it's base type:
1
2
3
4
5
6
7
8
9
10
11
class B{ };
class D : public B { }

class Foo
{
...
public:
	D member;
};

B Foo:: * pm = &Foo::member; // error, can't convert to B Foo::* 

Please can anyone explain why this is a problem & how I can work around it?
Thanks

I dont get what you are achieving by B Foo:: * pm = &Foo::member;

If you want to declare pointer to B then B *pm is the code.

And you cant just use scope resolution operator to get pointer to a member, you need to allocate memory/construct and object the class.
1
2
Foo f;
B *pm = &(f.member);


is the code if I have got what you want to achieve.
There is no member of Foo (or Foo's base) that has type B. A pointer of type B Foo::* cannot be valid.

What are you trying to work around? Perhaps you simply needed Foo foo; B* pm = &foo.member?
Last edited on
closed account (z0My6Up4)
thanks
Topic archived. No new replies allowed.