template; Point<2, double>; Point<3, double>
Posted
by Oops
on Stack Overflow
See other posts from Stack Overflow
or by Oops
Published on 2010-05-06T07:08:10Z
Indexed on
2010/05/06
7:18 UTC
Read the original article
Hit count: 192
Hi,
I want to create my own Point struct it is only for purposes of learning C++.
I have the following code:
template <int dims, typename T>
struct Point {
T X[dims];
Point(){}
Point( T X0, T X1 ) {
X[0] = X0;
X[1] = X1;
}
Point( T X0, T X1, T X2 ) {
X[0] = X0;
X[1] = X1;
X[2] = X2;
}
Point<dims, int> toint() {
//how to distinguish between 2D and 3D ???
Point<dims, int> ret = Point<dims, int>( (int)X[0], (int)X[1]);
return ret;
}
std::string str(){
//how to distinguish between 2D and 3D ???
std::stringstream s;
s << "{ X0: " << X[0] << " | X1: " << X[1] << " }";
return s.str();
}
};
int main(void) {
Point<2, double> p2d = Point<2, double>( 12.3, 45.6 );
Point<3, double> p3d = Point<3, double>( 12.3, 45.6, 78.9 );
Point<2, int> p2i = p2d.toint(); //OK
Point<3, int> p3i = p3d.toint(); //m???
std::cout << p2d.str() << std::endl; //OK
std::cout << p3d.str() << std::endl; //m???
std::cout << p2i.str() << std::endl; //m???
std::cout << p3i.str() << std::endl; //m???
char c; std::cin >> c;
return 0;
}
of couse until now the output is not what I want. my questions is: how to take care of the dimensions of the Point (2D or 3D) in member functions of the Point?
many thanks in advance
Oops
© Stack Overflow or respective owner