How variable is initialized by default constructor in c++

Viewed 1530

Given the following function template from a tutorial.

template<class T>
double GetAverage(T tArray[], int nElements)
{
    T tSum = T(); // tSum = 0

    for (int nIndex = 0; nIndex < nElements; ++nIndex)
    {
        tSum += tArray[nIndex];
    }

    // Whatever type of T is, convert to double
    return double(tSum) / nElements;
}

In the line

T tSum = T(); // tSum = 0

they are saying it will call the default constructor for particular type (based on the type we call this function). My doubt is how this call assigning the value to tSum, as this will call the constructor fine. but since constructor does not return anything how iSum is initialized to 0 for int or 0.0 for double.

3 Answers
Related