In the presented code, the following happens:
one)
template <typename T> void swapRows(T **matrix, int rowsQuantity) {
This template function takes the first argument to a pointer to a pointer to some type. Apparently, it is assumed that this type is elementary. For example, int , float , double .
Based on the fact that matrix is a pointer to a pointer, your matrix is represented as an array, which stores pointers to arrays (strings), in which the elements themselves are stored:

Where matrix is a pointer that stores the address of the ABC array;
2)
for (int i = 0; i < rowsQuantity - 1; i++) {
The whole matrix is traversed by rows. Two problems are immediately visible here:
- There is no control over the value of the
rowsQuantity variable; - A variable of type
int not quite suitable for addressing the elements of an array. A variable of type size_t fits much better.
3)
if ( i % 2 == 0 ) {
Actions are performed when посещении each line whose index is a multiple of two (0, 2, 4, etc.). And the actions, respectively, are performed in pairs over the lines 0-1, 2-3, 4-5. This is not entirely rational, because this approach performs twice the increments of the variable i , twice the number of divisions with the remainder and twice more comparisons than is really necessary.
It would be much more rational to perform i += 2 , generally discarding the division with the remainder and checking. But this approach will require checking the integer overflow of variable i .
four)
T &temp = *matrix[i]; matrix[i] = matrix[i+1]; matrix[i+1] = &temp;
At first glance, this is a rather idiomatic notation necessary for the classical exchange of places of two values using an intermediate variable:
temp = x; x = y; y = temp;
But not really. I'm not sure until the end what is happening in the depths of the compiler, and what the Стандарт says about this.
matrix[i] - gets the value of the pointer to the matrix row (the address of the array with the string values). * - dereference of the given address, getting the value (s) of the entire line. Apparently, the value of the string is placed in the reference variable temp .
I am not a C++ expert, but this code seems to me very strange. Logically, a link is the essence of compile time, in other words, it is just an all-name for something. That is, these three strange lines can be reduced to two:
x = y; y = x;
As a result, both in x and y will be the original value of y . But, apparently, the compiler basically understands what you want from it. Or maybe this is an indefinite behavior. I can't say for sure, because the current C++ takes up almost 2000 pages.
five)
matrix[i+1] = &temp;
This is where the data address associated with the temp link is used.
Ps. I would recommend mixing C++ with its C subset as rarely as possible. Not because C bad, but because such mixing has a number of significant drawbacks.
First, low-level C explicitness is incompatible with high-level implicit C++ . This is guaranteed to lead to difficult to understand errors, even in a completely trivial code. It will also force you to write even more code than using C alone - this is a paradox.
Secondly, the mixing of links with pointers, templates with macros, and exceptions with return codes leads to situations that cannot be analyzed, because the named elements are inconsistent in details, although they seem superficially very similar.
T &temp = *matrix[i];- absolutely not clear. This only confuses the code. You could just writeT *temp = matrix[i]; matrix[i] = matrix[i+1]; matrix[i+1] = temp;T *temp = matrix[i]; matrix[i] = matrix[i+1]; matrix[i+1] = temp;. And you just got a piece of code with an "even number of absurdities", as a result of which they compensate each other. - AnTC++, but notC- MGNeo