There is a vector implementation, not from std space, but approximately similar in functionality. Here are some excerpts of the server and client.

Vect.h file

#pragma once #include <iostream> #include <string> #include "VectError.h" template<typename T>class Vect { public: explicit Vect() :first(0), last(0) {} explicit Vect(size_t _n, const T& _v = T()) {//интересующая часть, как //работает конструктор T(), откуда он?? Allocate(_n); for (size_t i = 0; i < _n; ++i) *(first + i) = _v; } Vect(const Vect&); Vect& operator=(const Vect&); ~Vect() { #ifdef DEBUG cout << "Destructor of " << markName << endl; #endif // DEBUG Destroy(); first = 0, last = 0; } void mark(std::string& name) { markName = name; } std::string mark()const { return markName; } size_t size()const; T* begin()const { return first; } T* end()const { return last; } T& operator[](size_t i); void insert(T* _P, const T& _x); void push_back(const T& _x); void pop_back(); void show()const; protected: void Allocate(size_t _n) { first = new T[_n * sizeof(T)]; last = first + _n; } void Destroy() { for (T* p = first; p != last; ++p)p->~T(); delete[] first; } T* first; T* last; std::string markName; }; 

Excerpts from main.cpp

 #include "stdafx.h" #include <iostream> #include <conio.h> #include "Vect.h" #include <string> ... int main() { try { Vect<int> v1(10);// вызывается конструктор //explicit Vect(size_t _n, const T& _v = T())... v1.mark(string ("v1")); size_t n = v1.size(); for (int i = 0; i < n; ++i)v1[i] = i + 1; v1.show(); .... } 

It is not clear why in Vect<int> v1(10) _v in the expression const T& _v = T() by default equal to zero, (from where?) And why you can ignore the full fill in general, because in my opinion there is no corresponding constructor overload. Thank you in advance.

    1 answer 1

    This constructor

     explicit Vect(size_t _n, const T& _v = T()) {//интересующая часть, как //работает конструктор T(), откуда он?? Allocate(_n); for (size_t i = 0; i < _n; ++i) *(first + i) = _v; } 

    has a default argument for the second parameter, which is equal to the value of the T() expression. Therefore, you can call this constructor without specifying the second argument for the constructor, as in your example

     Vect<int> v1(10); 

    The second argument will be the default value that is present in the constructor declaration.

    The T() expression is called initialization value. For fundamental types, such initialization corresponds to object initialization with zero.

    From standard C ++ (8.5 Initializers)

    10 of the parentheses, ie, (), be value-initialized

    And further

    7 To value-initialize an object of type T means:

    ... - otherwise, the object is zero-initialized .

    And finally

    5 To zero-initialize an object or reference of type means:

    - if it is a scalar type (3.9), it is taken as an integral constant expression, converted to T;

    Consider the following simple example.

     #include <iostream> void f(int x = 10, int y = int()) { std::cout << "x = " << x << ", y = " << y << std::endl; } int main() { f(); f(1); f(1, 2); } 

    The output of this program to the console will look like this.

     x = 10, y = 0 x = 1, y = 0 x = 1, y = 2