ForeverStudent
ForeverStudent

Reputation: 2537

is this a typo or am i missing something?

Good day, I am going over Bjarne Stroustrup's "The C++ programming language" and I am facing a snippet of code which I think should be illegal, but is presented in the text.

I am wondering if this is merely a slight oversight, or if there is something that I am missing.

starting from chapter 3 page 63: we have user defined type Vector 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;
    };

notice there are 2 constructors for Vector, one with only size, another with a full initializer list looking like {2.0, 3.1, 4}

now we go on 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 
    };

and at this point Bjarne wants to demonstrate the "abstractness" of Container by defining a concrete class that inherits from Container, and happens to use a Vector field for its internal use. here it is:

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

notice that Vector_container, uses a Vector object internally. this class has only one explicit constructor that takes in the size of the container (and uses that size to pass in the constructor of its internal Vector).

now recall that Vector has yet another constructor of the form: Vector(std::initializer_list)

this is all fine and dandy but what confuses me is that the textbook goes on to present this code:

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

as you can see, vc which is an instance of Vector_container, is being passed an initializer list to its constructor. this confuses me. there was no mention of such constructor in the definition of Vector_container.

what am I missing here? Thanks

Upvotes: 4

Views: 295

Answers (1)

CoffeDeveloper
CoffeDeveloper

Reputation: 8315

Maybe oversight of book writers. There should be no problem creating the constructor yourself though:

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

You are actually right in saying that actualy something is missing on the book.

Upvotes: 6

Related Questions