informatique 5
Les classes suivantes
permettent de stocker des objets de différents types. Le but est d’utiliser un
seul type de constructeur en utilisant le type de données et l’objet en
question.
//////////////////////////////////////////////
//
ContainerT ////////////////////////
/////////////////////////////////////////////
template<class
T> class ContainerT
{
public:
inline ContainerT()
{
}
inline ContainerT(const T value)
{
itsValue = value;
}
inline ContainerT(const
ContainerT<T>& cont)
{
if (this != &cont)
{
itsValue =
cont.itsValue;
}
}
inline ContainerT<T>&
operator=(const ContainerT& cont)
{
itsValue = cont.itsValue;
return *this;
}
inline T GetValue()
{
return itsValue;
}
inline void SetValue(T value)
{
itsValue = value;
}
private:
T itsValue;
};
////////////////////////////////////////////////////////
// ContainerMatrixT
////////////////////////
///////////////////////////////////////////////////////
template<class T> class
ContainerMatrixT
{
public:
inline ContainerMatrixT()
{
}
inline ContainerMatrixT(const matrix<T>&
value)
{
itsValue = value;
}
inline ContainerMatrixT(const
ContainerMatrixT<T>& cont)
{
if (this != &cont)
{
itsValue =
cont.itsValue;
}
}
inline
ContainerMatrixT<T>& operator=(const ContainerMatrixT&
cont)
{
itsValue = cont.itsValue;
return *this;
}
inline matrix<T>& GetValue()
{
return itsValue;
}
inline void
SetValue(matrix<T>& value)
{
itsValue = value;
}
private:
matrix<T> itsValue;
};
////////////////////////////////////////////////////////////////////
//
Container: classe de base /////////////////////
///////////////////////////////////////////////////////////////////
template<class
T> class Container
{
public:
inline Container()
{
}
inline Container(int type)
{
itsType = type;
}
inline Container(int type, T
value)
{
itsType = type;
itsT = ContainerT<T>(value);
}
inline Container(int type,
vector<T>& value)
{
itsType = type;
itsVectorT =
ContainerVectorT<T>(value);
}
inline Container(int type,
matrix<T>& value)
{
itsType = type;
itsMatrixT = ContainerMatrixT<T>(value);
}
inline Container(const
Container& cont)
{
if (this !=
&cont)
{
itsType =
cont.itsType;
itsT =
cont.itsT;
itsVectorT = cont.itsVectorT;
itsMatrixT = cont.itsMatrixT;
}
}
inline Container& operator=(const
Container& cont)
{
itsType = cont.itsType;
itsT = cont.itsT;
itsVectorT = cont.itsVectorT;
itsMatrixT = cont.itsMatrixT;
return *this;
}
inline int GetType()
{
return itsType;
}
inline void SetType(int type)
{
itsType = type;
}
inline T GetT()
{
return itsT.GetValue();
}
inline vector<T>& GetVectorT()
{
return itsVectorT.GetValue();
}
inline matrix<T>& GetMatrixT()
{
return itsMatrixT.GetValue();
}
private:
int itsType;
ContainerT<T> itsT;
ContainerVectorT<T> itsVectorT;
ContainerMatrixT<T> itsMatrixT;
};
0/10 sur 0 vote
Sélectionnez une note dans le menu déroulant.
Aucun commentaire
Dernière mise à jour de cette page le 21/03/2008