H
hogtiedtoawaterbuffalo
I have a template class that works fine when I implement it with <int>,
but when I use <float> or <double> it doesn't work. The class has a
dynamic array of type T that gets instantiated in my constructor. When
type T is int, the array works like I expect. But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
I'm using Visual Studio 2005 on an XP pro machine. The following is
the code that is giving me problems. If anyone can help explain what
I'm doing wrong and why this doesn't work, I'd really appreciate it.
Thanks in advance.
template <class T>
class Tuple
{
public:
Tuple<T>(void);
Tuple<T>(T x, T y);
~Tuple<T>(void);
T getX();
T getY();
void setX(T x);
void setY(T y);
private:
T *_values; //arrray of values
};
template <class T>
Tuple<T>::Tuple(void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}
template <class T>
Tuple<T>::Tuple(T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}
template <class T>
Tuple<T>::~Tuple(void)
{
}
template <class T>
T Tuple<T>::getX() { return _values[0]; }
template <class T>
T Tuple<T>::getY() { return _values[1]; }
template <class T>
void Tuple<T>::setX(T x) { _values[0] = x; }
template <class T>
void Tuple<T>::setY(T y) { _values[1] = y; }
but when I use <float> or <double> it doesn't work. The class has a
dynamic array of type T that gets instantiated in my constructor. When
type T is int, the array works like I expect. But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
I'm using Visual Studio 2005 on an XP pro machine. The following is
the code that is giving me problems. If anyone can help explain what
I'm doing wrong and why this doesn't work, I'd really appreciate it.
Thanks in advance.
template <class T>
class Tuple
{
public:
Tuple<T>(void);
Tuple<T>(T x, T y);
~Tuple<T>(void);
T getX();
T getY();
void setX(T x);
void setY(T y);
private:
T *_values; //arrray of values
};
template <class T>
Tuple<T>::Tuple(void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}
template <class T>
Tuple<T>::Tuple(T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}
template <class T>
Tuple<T>::~Tuple(void)
{
}
template <class T>
T Tuple<T>::getX() { return _values[0]; }
template <class T>
T Tuple<T>::getY() { return _values[1]; }
template <class T>
void Tuple<T>::setX(T x) { _values[0] = x; }
template <class T>
void Tuple<T>::setY(T y) { _values[1] = y; }