简体   繁体   English

C ++ typename为变量

[英]C++ typename as variable

Suppose I have a template class MyClass. 假设我有一个模板类MyClass。 Is it possible to store the type of the template as an variable of the class? 是否可以将模板的类型存储为类的变量? If so how? 如果是这样的话? I'm curious if it's possible to do something like this. 我很好奇是否可以做这样的事情。

Template <typename T> class MyClass;
Template <typename T> class AnotherClass;

MyClass<int> myClassInstance;
AnotherClass<oneInstance.functionThatReturnsTheStoredTypeFromAnInstanceVariableOfMyClass()> anotherClassInstance;

Thanks in advance. 提前致谢。

They can be static variables, you can nest typedefs. 它们可以是静态变量,可以嵌套typedef。

template<typename T> class MyClass {
public:
    typedef T type;
};
AnotherClass<MyClass<double>::type> anotherClassInstance;

However, accessing a member variable like you posted is impossible in C++. 但是,在C ++中访问像您发布的成员变量是不可能的。

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

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