首页 > 解决方案 > C++入门加Chapter14 No-type Stack

问题描述

为什么 ArrayTP<double,12> eggweights; ArrayTP<double,13> donuts;生成两个单独的类,但Stack<int> eggs(12); Stack<int> dunkers(13)只生成一个声明?他们不应该是两个堆栈:一个是鸡蛋堆栈,另一个是扣篮

标签: c++templates

解决方案


嗯,这就是你的声明所说的。

ArrayTP<double,12> eggweights;
ArrayTP<double,13> donuts;

第一个变量的类型是ArrayTP<double,12>,而第二个变量的类型是ArrayTP<double,13>。这些是不同的。

Stack<int> eggs(12);
Stack<int> dunkers(13);

第一个变量的类型是Stack<int>,而第二个变量的类型是Stack<int>。这些都是一样的。

1213for是它们的构造函数的eggs参数。dunkers期望来自不同的类型就像期望以下的不同类型:

std::string foo{"hello"};
std::string bar{"world"};

推荐阅读