Derived class' pointer

I'm having trouble with this one...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class B 
{
private:
int b;
}

class D1 : public B
{
private:
int d1;
}

class D2 : public B
{
private:
int D1::* p;
}


And how to make pointer p point to d1?
d1 needs to be public:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class B 
{
private:
int b;
};

class D1 : public B
{
public:
D1():d1(10){}
int d1;
};

class D2 : public B
{
public:
int D1::* p;
};

int main()
{
	D2 a;
	D1 b;
	a.p = &D1::d1;
	std::cout<< b.*(a.p);
	std::getchar();
}


If that's not an option you'll need to use a getter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class B 
{
private:
int b;
};

class D1 : public B
{
public:
D1():d1(10){}
int get(){return d1;}
private:
int d1;
};

class D2 : public B
{
public:
int (D1::*p)();
};

int main()
{
	D2 a;
	D1 b;
	a.p = &D1::get;
	std::cout<< (b.*(a.p))();
	std::getchar();
}
Topic archived. No new replies allowed.