Hello,
I am curious what the following code is doing with the parameters. Since in main, the a is essentially a pointer to the first element in the integer array, what happens when displayElements is called and the first parameter is const T array (pass-by-value)? I found this works, as well as having the first parameter set as const T *array (pass-by-pointer). What's the difference, and since they both work, is one better than the other?
template <class T>
void displayElements(const T array, const int count)
{
for (int i = 0; i < count; i ++)
cout << array[i]<<" ";
cout << endl;
}
main()
{
int aCount = 3;
int a[aCount];
for (int i = 0; i< aCount; i++)
a[i] = i+1;
displayArray(a,aCount);
}
template <class T>
void displayElements(const T array, constint count) {...}
and
1 2
template <class T>
void displayElements(const T *array, constint count){...}
?
Well, in the first T will mean an int* and int the second, T will mean int, but since you don't use T anywhere in the function body, there is no difference at all.