我一直在研究由其他人(誰離開公司)啓動的C++項目。他寫了一段代碼,似乎工作得很好,但我無法理解它。C++ 11 /生成的構造函數
這裏是下面的代碼的簡化版本:
有兩類:
class Algo_t {
protected :
Matrix_t m_Matrix ;
public:
Algo_t(Matrix_t && Matrix) {
DoSomething();
}
};
class Matrix_t {
protected :
std::ifstream & m_iftsream ;
public:
Matrix_t(std::ifstream && ifstream) {
DoSomething();
}
};
在主:
有在主函數下面的調用:
char * pMyFileName = agrv[1] ;
Algo_t MyAlgo(ifstream(pMyFileName));
首先,我非常驚訝的是,代碼編譯沒有任何錯誤becau se沒有構造函數Algo_t
以ifstream
作爲參數。 我更驚訝地發現這個代碼工作得很好。
構造函數是由編譯器生成還是由C++ 11引入了一些新特性(使用右值...)?
也許類'Matrix_t'有一個'ifstream'鑄造運營商,或需要'ifstream'的構造函數?哦,嘿,它確實...這就解釋了它...... –
即使修復了一些明顯的錯誤,您的簡化版本也不會編譯。 –
這與你構造一個帶'const char []'的'std :: string'的結構(幾乎) 'std :: stringstream(「Hello World!」)' - 你不需要顯式地執行'std :: stringstream(std :: string(「Hello World!」))''。 – Holt