1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <iostream>
#include <boost/shared_ptr.hpp>
struct ColisNu
{
int poids;
int taille;
ColisNu(){
std::cout<<"Creation colis nu"<<std::endl;
}
ColisNu(ColisNu const &){
std::cout<<"Creation d'une copie de colis nu"<<std::endl;
}
~ColisNu()
{
std::cout<<"Destruction d'un colis nu"<<std::endl;
}
private:
ColisNu&operator=(ColisNu const&);
};
class Colis{
public:
Colis()
:colis_nu(new ColisNu)
{std::cout<<"Creation d'un colis"<<std::endl;}
Colis(boost::shared_ptr<ColisNu> const &ptr_colis_nu)
:colis_nu(ptr_colis_nu)
{std::cout<<"Creation d'un colis"<<std::endl;}
Colis(Colis const &rhs_)
:colis_nu(rhs_.colis_nu)
{std::cout<<"Creation d'un colis"<<std::endl;}
virtual int get_destination() const=0;
virtual ~Colis()=0;
boost::shared_ptr<ColisNu> colis_nu;
private:
Colis&operator=(Colis const&);
};
Colis::~Colis()
{
std::cout<<"Destruction d'un colis"<<std::endl;
}
class Colis_Fragile: public Colis {
public:
Colis_Fragile(boost::shared_ptr<ColisNu> const &ptr_colis_nu):Colis(ptr_colis_nu){}
int get_destination()const{
std::cout<<"je suis fragile !"<<std::endl;
return 1;
}
int pression_max;
};
class Colis_Normal: public Colis{
public:
Colis_Normal(boost::shared_ptr<ColisNu> const &ptr_colis_nu):Colis(ptr_colis_nu){}
int get_destination()const{
std::cout<<"je suis normal !"<<std::endl;
return 2;
}
};
class Colis_Solide: public Colis{
public:
Colis_Solide(boost::shared_ptr<ColisNu> const &ptr_colis_nu):Colis(ptr_colis_nu){}
int get_destination()const{
std::cout<<"je suis solide !"<<std::endl;
return 3;
}
char *type_de_materiau;
};
enum E_Type_Colis{
TYPE_FRAGILE,
TYPE_NORMAL,
TYPE_SOLIDE
};
template<class istreamT>
istreamT& operator>>(istreamT &in_, E_Type_Colis&val_)
{
int i;
in_>>i;
switch(i){
case 0:
val_ = TYPE_FRAGILE;
break;
case 1:
val_ = TYPE_NORMAL;
break;
case 2:
val_ = TYPE_SOLIDE;
break;
default:
throw "valeur invalide";
break;
}
return in_;
}
#include <boost/shared_ptr.hpp>
class FabriqueColis
{
public:
boost::shared_ptr<Colis> CreerColis(E_Type_Colis type_,boost::shared_ptr<ColisNu> const &ptr_colis_nu_) const
{
switch(type_){
case TYPE_FRAGILE:
return boost::shared_ptr<Colis>(new Colis_Fragile(ptr_colis_nu_));
break;
case TYPE_NORMAL:
return boost::shared_ptr<Colis>(new Colis_Normal(ptr_colis_nu_));
break;
case TYPE_SOLIDE:
return boost::shared_ptr<Colis>(new Colis_Solide(ptr_colis_nu_));
break;
default:
throw;
}
}
};
void TraiterColis(boost::shared_ptr<Colis>&colis_)
{
colis_->get_destination();
}
int main()
{
boost::shared_ptr<ColisNu> mon_colis_nu(new ColisNu);
E_Type_Colis type;
std::cout<<"De quel type est le colis (0 fragile, 1 normal, 2 solide) ?";
std::cin>>type;
FabriqueColis ma_fabrique;
boost::shared_ptr<Colis> mon_colis = ma_fabrique.CreerColis(type,mon_colis_nu);
TraiterColis(mon_colis);// OK
//TraiterColis(mon_colis_nu);// Erreur à la compilation
return 0;
} |
Partager