简体   繁体   English

如何将模板 class 的内部 class 分离到其他文件中

[英]How to separate inner class of template class into other file

I want to do:我想要做:

typedef MyTemplateClass<Type01, Type02> TClass;
TClass t;
TClass::InnerClass i;
i.test();

i think the solution could be:我认为解决方案可能是:

template <typename A, typename B>
class MyTemplateClass
{
public:
    class InnerClass
    {
        //here I can do stuff with A and B
        A test() { return 0; }
    };

    friend class InnerClass;
};

but I want to have ma templates in separate *.inl file included at the bottom of header file但我想在 header 文件底部包含单独的 *.inl 文件中包含 ma 模板

how to define such behaviour in other file?如何在其他文件中定义这种行为?

when i do just当我这样做的时候

//file.inl
class InnerClass
{
    //here I can do stuff with A and B
    A test() { return 0; }
};

A and B are undefined. A 和 B 未定义。

but

template <typename A, typename B>
class InnerClass
{
...
};

makes my method template independent of MyTemplateClass types...使我的方法模板独立于 MyTemplateClass 类型...

Once again in one sentence: How to make InnerClass of MyTemplateClass in other file with providing再说一遍:How to make InnerClass of MyTemplateClass in other file with provide

TClass::InnerClass i;
i.test();

behaviour?行为?

It works the same as when using a non-template: you have to fully qualify the name of the member:它与使用非模板时的工作方式相同:您必须完全限定成员的名称:

struct outer {
    struct nested;
};

struct outer::nested {
    int
    test();
};

int
outer::nested::test()
{ /* whatever */ }

In your case that'd be:在你的情况下是:

template <typename A, typename B>
class MyTemplateClass {
public:
    class InnerClass;

    friend class InnerClass;
};

// note the use of the <> brackets
template<typename A, typename B>
class MyTemplateClass<A, B>::InnerClass {
public:
    A
    test();
};

// ditto
template<typename A, typename B>
A
MyTemplateClass<A, B>::InnerClass::test()
{ return 0; }

You do have to be wary of the definition order though: if eg MyTemplateClass has function members that use InnerClass , then the definition of InnerClass has to be visible at the point of use.但是,必须对定义顺序保持警惕:例如,如果MyTemplateClass具有使用 InnerClass 的InnerClass成员,则InnerClass的定义必须在使用时可见。

Simple rule of thumb: inline everything or nothing.简单的经验法则:内联所有内容或什么都不内联。 Either you define everything inline (the InnerClass class definition and the function members of both classes) inside MyTemplateClass , or you put all the definitions of all function members (of both MyTemplateClass and InnerClass ) at the very end after InnerClass has been defined. Either you define everything inline (the InnerClass class definition and the function members of both classes) inside MyTemplateClass , or you put all the definitions of all function members (of both MyTemplateClass and InnerClass ) at the very end after InnerClass has been defined.

Don't fret though, if you mess up your compiler will only be too happy to help you with error messages.不过不要担心,如果你搞砸了你的编译器只会很乐意帮助你处理错误消息。

You can't.你不能。 In a nutshell.简而言之。 Templates must be wholly defined before instantiation- that means that whatever you do, you'll have to define the inner class in the template class anyway.模板必须在实例化之前完全定义 - 这意味着无论您做什么,您都必须在模板 class 中定义内部 class 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM