我对创建模板类时发生的事情感到有些困惑。我试图在构造过程中将成员 vector_ 的容量设置为 50(仅一次),但似乎容量从未正确设置,所以显然我不明白应该如何做到这一点。我已经包含了相关的代码片段以及控制台输出。感谢您的帮助!
向量的模板类:
template <typename T>
class V
{
public:
V()
{
std::cout << "capacity 1 = " << this->vector_.capacity() << "\n";
};
V(int capacity)
{
this->vector_.reserve(capacity);
std::cout << "capacity 2 = " << this->vector_.capacity() << "\n";
};
int capacity() const { return this->vector_.capacity(); };
private:
std::vector<T> vector_;
};
初始化 R 的构造函数:
R::R()
{
std::cout << "capacity 0 = " << this->s_.capacity() << "\n";
this->s_ = V< std::vector< std::complex<float> > >(50);
std::cout << "capacity 3 = " << this->s_.capacity() << "\n";
};
R类的标题:
class R
{
public:
R();
private:
V< std::vector< std::complex<float> > > s_;
};
输出到控制台:
capacity 1 = 0
capacity 0 = 0
capacity 2 = 50
capacity 3 = 0