A类有一个可以共享的对象,但只能在内部修改
class A
{
public:
validityClass ** getValidityShared() // I want to make this const return, not sure how to protect a double pointer const signeture
{
return &validityShared;
}
private:
validityClass * validityShared;
}
B类需要通过其构造函数
进行访问,读取验证或B类填充class B
{
B(A & ainst)
{
aValidityShared = ainst.getValidityShared();
}
private:
validityClass ** aValidityShared;/// tried to save it as validityClass ** & aValidityShared but I guess its wrong, even though everything worked out
}
B构造函数结束后,我不再需要了解A类的任何内容!
int main(int argc, char* argv[])
{
A instA;
B(&instA);
return EXIT_SUCCESS;
}
我希望B intance拥有A共享变量,但没有任何额外资源,所以我尝试将其保存为rValue。
我问这个问题,因为我收到了一些奇怪的警告:
member initializers for
const char ** classB :: xVar`
答案 0 :(得分:1)
validityClass ** getValidityShared()
// I want to make this const return, not sure how
// to protect a double pointer const signeture
您需要在值和指针中添加const,以便提供签名的真正const正确性:
validityClass const * const * getValidityShared() const
第二个指针不必是const,因为它是一个值。我将const
放在右边,因为那是正确的要做的事情,我相信它会让它更具可读性。
您还可以返回引用,在这种情况下您需要:
validityClass const * const & getValidityShared() const