私有继承的直接基class防止派生class定义间接基class的对象

The direct base class of private inheritance prevents the derived class from defining the object of the indirect base class

喜欢这个代码

class Try
{
public:
    Try() = default;
    int i = 0;
};

class B1 : private Try
{
public:
    B1() = default;
    using Try::Try();
    using Try::i;
};

class C1 : public B1
{
public:
    Try a; //tell me:'Try' is a private member of 'Try'
    
    void print()
    {std::cout << i << std::endl;}
    //Access to this I is allowed;
};

Try a是本地对象,不是C1的一部分,为什么会报错?

只要是私有继承的直接基class,难道不能在其派生class中定义一个间接基class对象吗?是构造函数不能使用还是其他原因?

the Try a is a local object , not a part of C1, why error?

通过在 class C1 的上下文中写入 Try a;,名称查找通常总是从本地范围扫描到全局范围。因此,第一个匹配项将是 B1::Try,由于私有继承,C1.

无法访问它

修复很简单,只需提示编译器您“真正”的意思,即通过编写例如::Try a;.