2012-04-09 44 views

回答

3

如果我明白你在做什麼之後,有一種可能性是使用包含其他函數的基類,然後有一個模板和一個專門化,該基類,專業化添加您想要的額外功能:

struct X { 
    int x() { return 1; } 
}; 

template<class T> 
struct Y : public X { 
}; 

template<> 
struct Y<int> : public X { 
    int y() { return 2; } 
}; 

int main() { 
    Y<long> y; 
    y.x(); 
    Y<int> z; 
    z.y(); 
    return 0; 
} 
相關問題