Explain C++ code

后端 未结 3 1074
执念已碎
执念已碎 2021-01-17 06:11

Can I get some help with explanation of the following code?

#include 

class Vector {
    private:
        double∗ elem; // pointer to the el         


        
3条回答
  •  囚心锁ツ
    2021-01-17 06:37

    Vector(int s) defines a constructor - a special method that is called when object is created.

    :elem{new double[s]}, sz{s} is a initializer list - it initializes object's fields. The whole part:

    Vector(int s):elem{new double[s]}, sz{s} {}
    

    Works same as

    Vector(int s) {    
    elem = new double[s];
    sz = s;
    }
    

    However, initializer lists can be used to initialize constants and references.

提交回复
热议问题