-1
我有一个类(C)和一个跟踪第三类(A)的成员类(B)。通过C和B调用A的公共成员函数的正确语法是什么?还是我搞砸了我的指针?通过多个指针调用成员函数
#include <iostream>
class A
{
public:
void Hello() const {std::cout<<"World."<<std::endl;};
};
class B
{
const A* aa; // Pointer can change, data cannot.
public:
const A* *const aaa; // Pointer and pointed value are const.
B() : aaa{&aa} {};
void SetPointerToA(const A& aRef) {aa = &aRef;};
};
class C
{
B b;
public:
B* bb; // Provide access to public members of B.
C() : bb{&b} {};
};
int main()
{
A aTop;
C c;
c.bb->SetPointerToA(aTop); // Tell c.b to modify itself. No problems here.
c.bb->aaa->Hello(); // <==== Does not compile.
return 0;
}
的gcc 5.2.0抱怨调用你好():
error: request for member 'A:: Hello' in '*(const A**)c.C::bb->B::aaa',
which is of pointer type 'const A*' (maybe you meant to use '->' ?)
@OlegBogdanov'aa'是私人。而'aaa'是一个双指针。 –
'(*(c-> bb-> aaa)) - > Hello()'。你提出了这个问题,我们可以说,非常规安排会员指向其他成员。 –
你为什么这么复杂。请记住[KISS](https://en.wikipedia.org/wiki/KISS_principle) –