I started reading the book 'The C++ Programming Language - 4th Edition' by Bjarne Struoustrup and found the following example bit confusing (Abstract Types - page 66):
class Container {
public:
virtual double& operator[](int) = 0; // pure virtual function
virtual int size() const = 0; // const member function (§3.2.1.1)
virtual ~Container() {} // destructor (§3.2.1.2)
};
class Vector_container : public Container { // Vector_container implements Container
Vector v;
public:
Vector_container(int s) : v(s) { } // Vector of s elements
~Vector_container() {}
double& operator[](int i) { return v[i]; }
int size() const { return v.size(); }
};
Client code:
void use(Container& c)
{
const int sz = c.size();
for (int i=0; i!=sz; ++i)
cout << c[i] << '\n';
}
void g()
{
Vector_container vc {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
use(vc);
}
Are we not missing the following constructor in the class declaration of Vector_container?
Vector_container(std::initializer_list<double> s) : v(s) { } // Vector of s elements
Please correct me if I misunderstood anything here.
Are we not missing the following constructor in the class declaration of Vector_container?
Vector_container(std::initializer_list<double> s) : v(s) { } // Vector of s elements
Of course you're right.
error: no matching constructor for initialization of 'Vector_container'
Vector_container vc {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};