Dok ne budes dobio detaljniji odgovor od nekog da probam ja da pomognem.
2. Sintaksa je:
Kod:
template < typename T >
typename Vec2d<T>::Vec2d Vec2d<T>::Zero = Vec2d<T>();
Da ne ulazim zasto (Google "c++ template class implementation"), obicno kod templejt klasa iplementacija metoda se pravi u samom telu te klase.
Evo ti recimo za operatore "uporedjivanja" (g++ 4.7), ovi operatori trebaju isto da budu
const posto ne menjaju clanove klase.
Kod:
#include <type_traits>
...
template < typename T >
class Vec2d
{
// primamo samo odredjene tipove templejta Vec2d
// ostalo je compile time error
static_assert( std::is_floating_point<T>::value || std::is_integral<T>::value, "Unacceptable type for Vec2d!");
public:
Vec2d() : x(0), y(0) {}
Vec2d(T _x, T _y) : x(_x), y(_y) {}
template < typename U >
typename std::enable_if< std::is_floating_point<T>::value || std::is_floating_point<U>::value, bool >::type
operator == (const Vec2d<U>& o) const
{
if((std::fabs(x - o.x) > 0.00001) || (std::fabs(y - o.y) > 0.00001))
{
return false;
}
return true;
}
template < typename U >
typename std::enable_if< std::is_integral<T>::value && std::is_integral<U>::value, bool >::type
operator == (const Vec2d<U>& o) const
{
return x == o.x && y == o.y;
}
bool operator != (const Vec2d<T>& o) const
{
return !(*this == o);
}
T x, y;
static Vec2d<T> Zero;
};
Overload operatora == zbog uporedjivanja floating point brojeva.
3. Daj celu poruku za sta kuka.