I am pretty confused by the output of following code:
#include <cmath>
#include <vector>
#include <iostream>
class V
{
std::vector<int> *ex_;
public:
V( std::vector<int>::size_type sz );
~V();
};
V::V( std::vector<int>::size_type sz )
{
// Why this doesn't work ??
ex_ = new std::vector<int>( sz );
std::cout<< "Ex size:" <<ex_->size() << std::endl;
}
V::~V()
{
delete ex_;
}
int main()
{
// This works
std::vector<int> *myVec = new std::vector<int>(10);
std::cout << "Vector size:" << myVec->size() << std::endl;
delete myVec;
// Why this doesn't work ??
V v(myVec->size());
return 0;
}
Output:
Vector size:10
Ex size:34087952
I had expected Ex size to be 10 and not the address of heap memory where vector is created on heap. What is it I am doing wrong here ?