※ 引述《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;