Yuan Wen
Yuan Wen

Reputation: 1701

How to make base template class functions visible in derived class?

In "Effective C++" Item 44:Factor parameter-independent code out of templates.I find some difference between its English version and Chinese Version translated by Hou Jie(侯捷).

This is the English Version I found in page 214:

template<typename T> // size-independent base class for
class SquareMatrixBase { // square matrices
protected:
...
void invert(std::size_t matrixSize); // invert matrix of the given size
...
};
template<typename T, std::size_t n>
class SquareMatrix: private SquareMatrixBase<T> {
private:
using SquareMatrixBase<T>::invert; // make base class version of invert
// visible in this class; see Items 33
// and 43
public:
...
void invert() { invert(n); } // make inline call to base class
}; // version of invert  

In its Chinese Version translated by Hou Jie(侯捷) .The previous lines of code are almost the same except the second last line of code:

void invert() { this->invert(n); }  

In the Chinese Version, Hou Jie explains the reason to use this->invert(n) instead of invert(n): the function names of templatized base classes will be hidden in the derived classes.
I think this may be wrong,because using SquareMatrixBase<T>::invert; has been added in other part of the derived class.

But I think as a famous translator,Hou Jie won't easily make such an obvious mistake.Is he really wrong this time?

Upvotes: 0

Views: 229

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129344

Both of these are equivalent. this->invert(n) and invert(n) will both call the same base-class function. I'm only 99% sure, but I don't think using SquareMatrixBase<T>::invert; will matter here, since there is no valid invert that takes an argument in the derived class.

Edit: Since this is a TEMPLATED class, you need the using statement or the this->invert(n) to make clear which invert to use. This is because there could also be a global invert that takes an argument, and the compiler can't really know which you wish to use.

Obviously it's impossible for anyone here to say WHY it's done this way - it could be that the translation is from an older version of the book, where this has been updated later on by the author. Often when translations are done, the translator gets a "preview version" of the final document, so the translation can be released near the date of the original language. Then updates are sent out (you hope!), and the translator updates the translated version. Given that there are humans involved here, it's possible that mistakes creep in at some stage of this process.

Upvotes: 2

Related Questions