【问题标题】:Why doesn't the following CRTP hierarchy compile?为什么以下 CRTP 层次结构无法编译?
【发布时间】:2017-08-29 17:32:39
【问题描述】:

我正在尝试实现类的CRTP 层次结构。我对基类感兴趣,以便在链中访问派生类的数据成员:

#include <iostream>

template <class Derived>
class A {
public:
    void showv() {
        std::cout << static_cast<const Derived*>(this)->v << std::endl;
    }
};

template <class Derived>
class B : public A< Derived > {
    typedef A<Derived> base;
    friend base;
};

class fromA : public A<fromA> {
    typedef A<fromA> base;
    friend base;
protected:
    int v = 1;
};

class fromB : public B<fromB>
{
    typedef B<fromB> base;
    friend base;
protected:
    int v = 2;
};

int main()
{
    // This runs ok
    fromA derived_from_a;
    derived_from_a.showv();

    // Why doesn't the following compile and complains about the protected member?
    fromB derived_from_b;
    derived_from_b.showv();

    return 0;
}

Demo

虽然第一个派生类 (fromA) 可以按预期编译和运行,但派生自A 的类的第二个派生类 (fromB) 却没有。

  1. 朋友声明未通过的原因是什么?
  2. 对解决方法有什么建议吗?

【问题讨论】:

  • 为什么不创建虚函数 getV() 并在每个派生类中重写它,而不是和朋友一起玩?
  • friend 我的friend 不是我在 C++ 中的朋友。

标签: c++ polymorphism hierarchy friend crtp


【解决方案1】:

问题是:我朋友的朋友不是我的朋友。

fromA 你有

typedef A<fromA> base;
friend base;

这使得A&lt;fromA&gt; 成为朋友并且show 可以访问fromA 的受保护成员。

fromB 你也有

typedef B<fromB> base;
friend base;

但这不会让A 成为朋友,而是让B 成为你的朋友。尽管 A 是 B 的朋友,但这并不意味着它现在也是 fromB 的朋友,这就是为什么您无法在 show 中访问 v

解决此问题的一种方法是将typedef A&lt;Derived&gt; base; 设为公开或在B 中受保护,然后在fromB 中添加friend base::base;,这将授予A 访问权限。

【讨论】:

  • 它有效,谢谢!出于某种原因,我认为base 将其结果一直链接到fromB
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 2016-10-27
  • 1970-01-01
相关资源
最近更新 更多