關於這裏已經提到我的Point結構:
template class: ctor against function -> new C++ standard
是有機會與鑄造運營商(INT),以取代功能toint()?模板;運營商(INT)
namespace point {
template < unsigned int dims, typename T >
struct Point {
T X[ dims ];
//umm???
template < typename U >
Point< dims, U > operator U() const {
Point< dims, U > ret;
std::copy(X, X + dims, ret.X);
return ret;
}
//umm???
Point< dims, int > operator int() const {
Point<dims, int> ret;
std::copy(X, X + dims, ret.X);
return ret;
}
//OK
Point<dims, int> toint() {
Point<dims, int> ret;
std::copy(X, X + dims, ret.X);
return ret;
}
}; //struct Point
template < typename T >
Point< 2, T > Create(T X0, T X1) {
Point< 2, T > ret;
ret.X[ 0 ] = X0; ret.X[ 1 ] = X1;
return ret;
}
}; //namespace point
int main(void) {
using namespace point;
Point< 2, double > p2d = point::Create(12.3, 34.5);
Point< 2, int > p2i = (int)p2d; //äähhm???
std::cout << p2d.str() << std::endl;
char c; std::cin >> c;
return 0;
}
我認爲問題在於C++無法區分不同的返回類型嗎?提前謝謝了。 問候
哎呀
你的語法錯了。你想投射到int?你想投射到不同類型的點嗎? – Anycorn 2010-05-09 16:06:24