Is it a typo or am I missing something?

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; // elem points to an array of sz doubles
    int sz;

    public:
    Vector(int s) :elem{new double[s]}, sz{s} // constructor: acquire resources
    {
            for (int i=0; i!=s; ++i) elem[i]=0; // initialize elements
    }

    Vector(std::initializer_list<double>)
    {
    // initialize with a list
    } 
    // ...
    void push_back(double)
    { 
    // add element at end increasing the size by one
    }

    ~Vector() {
     delete[] elem; 
     } // destructor: release resources

    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; // pure virtual function
    virtual int size() const = 0; // const member function (pure virtual) 
    virtual ~Container() {} // destructor 
    };

"" , , Container, , , Vector . :

    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(); }
      };

, Vector_container Vector . , ( ).

, Vector : Vector (std:: initializer_list)

, , :

 void g()
 {
     Vector_container vc {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
     // use vc as a Container type
 }

, vc, Vector_container, . . Vector_container .

?

+4
1

, . , :

class Vector_container : public Container {
public:
    //...
    Vector_container( std::initializer_list< double> a)
        :v(a){
    }
    //...
}

, , - .

+6

Source: https://habr.com/ru/post/1615783/


All Articles