Good afternoon, I turn to Bjarne Stroustrup "C ++ Programming Language", and I came across a piece of code that, I think, should be illegal, but presented in the text.
I wonder if this is just a simple distrust, or if something is missing me.
starting with chapter 3, page 63: we have a custom Vector type as follows:
class Vector {
private:
double* elem;
int sz;
public:
Vector(int s) :elem{new double[s]}, sz{s}
{
for (int i=0; i!=s; ++i) elem[i]=0;
}
Vector(std::initializer_list<double>)
{
}
void push_back(double)
{
}
~Vector() {
delete[] elem;
}
double& operator[](int i);
int size() const;
};
note that for Vector there are 2 constructors, one with size, the other with a complete list of initializers similar to {2.0, 3.1, 4}
now we continue to define an abstract class called "Container", it looks like this:
class Container {
public:
virtual double& operator[](int) = 0;
virtual int size() const = 0;
virtual ~Container() {}
};
"" , , Container, , , Vector . :
class Vector_container : public Container {
Vector v;
public:
Vector_container(int s) : v(s) { }
~Vector_container() {}
double& operator[](int i) { return v[i]; }
int size() const { return v.size(); }
};
, Vector_container Vector . , ( ).
, Vector : Vector (std:: initializer_list)
, , :
void g()
{
Vector_container vc {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
}
, vc, Vector_container, . . Vector_container .
?