※ 引述《gn00618777 (非常念舊)》之銘言:
: 最近在 trace android 的 code ,裡面有些 c++ 的程式,寫了一小段來驗證
: #include<iostream>
: #include<stdlib.h>
: using namespace std;
: template<typename T>
: class Demo{
: public:
: Demo(T* other);
: Demo(const Demo<T>& other);
: };
: template<typename T>
: Demo<T>::Demo(T* other){
: cout<<"This is the First constructor"<<endl;
: }
: template<typename T>
: Demo<T>::Demo(const Demo<T>& other){
: cout<<"This is the second constructor"<<endl;
: }
: int main(){
: int *ptr;
: Demo<int> p; // 會 error,因為它會找不到 Demo() 建構子,這個合理~
: Demo<int> p = ptr; //這邊他會 call Demo(T* other) 這邊的建構子
: //不太懂為啥他會call 第一個建構子呢?
: //如果用 Demo<int> p(ptr); 還比較容易理解..
: }
: 謝謝
板友提供的關鍵字 copy initialization,讓我大概瞭解
int *ptr;
Demo<int> p = ptr;