thealch3m1st
thealch3m1st

Reputation: 282

C++ typename as variable

Suppose I have a template class 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.

Upvotes: 4

Views: 11174

Answers (1)

Puppy
Puppy

Reputation: 146910

They can be static variables, you can nest typedefs.

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++.

Upvotes: 8

Related Questions