繁体   English   中英

模板成员 function 与 stl

[英]Template member function with stl

我在 MyQueue.h 中有以下现有代码片段

class MyQueue {
 struct A {
  explicit A(ValType init){
 }
 ValType memberA;
 };
 struct B {
  explicit B {...}
  std::list<A> listofA; 
 };
 std::unordered_map<std::string, B> myMap;
};

ValType 是在不同文件中定义的已知 class 类型。

目标

我需要模板化 Valtype,这意味着 struct A 构造函数(或使用 Valtype 的任何地方)可以是 Valtype 或 Valtype2。

我的问题如下

  1. 整个 class MyQueue 是否需要成为模板 class 或者相关的成员变量和成员 function 可以像我下面那样被模板化。 决定这一点的一般规则是什么?

  2. stl 容器可以是模板类型吗? ex std::unordered_map<std::string, B<T>> myMap; std::list<A<T>> listofA;

这个 class 有一个 cpp 文件,其中定义了使用 A、B 和 myMap 的 class 成员函数。

class MyQueue {
 template <class T>
 struct A {
  explicit A(T init){
 }
 T memberA;
 };

 struct B {
  explicit B {...}
  template <class T>
  std::list<A<T>> listofA; 
 };
 template <class T>
 std::unordered_map<std::string, B<T>> myMap;
};

我将 class 制作为模板 class

template<class TypeName>
class MyQueue {
};

为了定义成员函数,我将定义从 MyQueue.cpp 移动到新创建的 MyQueue-inl.h 中,我将其包含在 MyQueue.h 的末尾

暂无
暂无

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

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