[問題] template 用法
最近在 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); 還比較容易理解..
}
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 58.115.110.72
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1460796115.A.CFA.html
推
04/16 21:25, , 1F
04/16 21:25, 1F
推
04/16 22:15, , 2F
04/16 22:15, 2F
→
04/16 22:15, , 3F
04/16 22:15, 3F
推
04/17 10:13, , 4F
04/17 10:13, 4F
→
04/17 19:53, , 5F
04/17 19:53, 5F
→
04/17 19:54, , 6F
04/17 19:54, 6F
→
04/17 19:54, , 7F
04/17 19:54, 7F
推
04/18 12:21, , 8F
04/18 12:21, 8F
→
04/18 12:21, , 9F
04/18 12:21, 9F
推
04/18 22:13, , 10F
04/18 22:13, 10F
→
04/18 22:13, , 11F
04/18 22:13, 11F
→
04/18 22:14, , 12F
04/18 22:14, 12F
討論串 (同標題文章)