【问题标题】:Correct syntax of making a "class template" friend to a class [closed]使“类模板”成为类朋友的正确语法[关闭]
【发布时间】:2013-02-01 15:07:57
【问题描述】:

我只需要:

使“类模板”成为类朋友的正确语法

【问题讨论】:

  • 你试过谷歌搜索吗?

标签: c++ syntax class-template


【解决方案1】:

如果你想要一个不同类型的朋友类:

template<typename,typename> friend class ClassName;

【讨论】:

  • 所以我需要使用 typename 关键字,然后使用模板参数的标识符?
  • 它将使任何模板化的 ClassName 版本成为朋友。
  • 所以如果我的类模板是:template class CT{};它在 C 类中的友元声明是:模板 友元类 CT;真的吗?
  • 是的。虽然您不必指定参数名称(T)......只需关键字“typename”
【解决方案2】:

给定一个类模板,例如

template <typename> class Template;

您可以与模板的特定专业化成为朋友:

friend class Template<int>;

或所有专业:

template <typename> friend class Template;

【讨论】:

    【解决方案3】:

    在 C++11 中你可以这样做:

    template<typename T>
    class C
    {
        friend T;
    };
    

    【讨论】:

    • 这不是让类模板成为朋友。这是让班级 (T) 成为朋友。
    • 你说得对,我误解了他想要的,:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2011-10-01
    相关资源
    最近更新 更多