I'm somewhat baffled as to what is happening when I create my template class. I'm trying to set the capacity of the member vector_ to 50 (only once) during construction, but it seems the capacity is never properly set, so obviously I do not understand how this is supposed to be done. I've included the relevant snippets of code, as well as the console output. Thank you for your help!
Template class for vector:
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_;
};
Constructor of R where it is initialized:
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";
};
Header of R class:
class R
{
public:
R();
private:
V< std::vector< std::complex<float> > > s_;
};
Output to console:
capacity 1 = 0
capacity 0 = 0
capacity 2 = 50
capacity 3 = 0
You don't set the size of the std::vector<T> but its capacity. The capacity isn't a salient attribute of std::vector<T> and, thus, not copied. You need to use resize() to set the size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With