[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [linux-team] [OUT OF TOPIC][C++] Lacunes en C++
On Tue, Jan 25, 2000 at 12:23:07PM +0100, Michael Stilmant wrote:
> Je refais du C++ après un petit temps et je tombe sur un os.
> J'aimerais créer une classe dont le constructeur initialise une
> variable avec l'appel d'une fonction.
> Mais voilà, je peux tester le retour de cette fonction, et je ne
> sais pas comment je doit réagir si cette fonction retourne une erreur et
> donc que la variable n'est pas initialisée. La variable n'étant pas
> initialisée, l'objet (classe) n'a plus de raison d'être.
Ben, tu jettes une exception évidemment...
...
> variable=fonction_qui_est_dans_une_librairie_pas_a_moi_qui_initialise_de
> s_trucs();
> if (variable==NULL) {
> // pas bonne initialisation
> // qu'est-ce que je fais
tu fais un throw, hein, m'fi ;))
...
> Maintenant je cherche un truc qui permet à la (aux) fonction(s)
> qui crée(nt) un object de type philo que ca c'est mal passé du coté
> constructeur.
>
> du genre
>
> philo *my_philo_variable;
>
> if ((my_philo_variable=new philo)== NULL) {
> // ok c'est ko
> }
// NB: ça fait longtemps, en effet... un nom de classe *doit* commencer
// par une majuscule (a moins de vouloir programmer comme un forain) ;))
#include <String.h>
class PhiloException
{
public:
String cause;
PhiloException(const char* s) : cause(s) {};
};
class Philo
{
private:
Foo* foo;
public:
Philo()
{
foo = fonction_qui_init_foo();
if (!foo) throw(PhiloException("zut! a pas marché"));
}
~Philo()
{
if (foo) fonction_qui_nique_foo();
}
};
Philo* pMyPhilo;
try {
pMyPhilo = new Philo;
} catch (PhiloException e) {
// crotte, a pas marche
cout << "philo is dead, cause: " << e.cause << endl;
};
> Si quelqu'un à quelques idées sur cette question philosophique.
Voilà ;)
--
-o) / Pascal Bleser ATOS Payment Systems|
/\\ \ C++/UNIX Development Aachen, Germany|
_\_v \<guru@linuxbe.org> <pbleser@atos-group.com>|
---------------------------------------------------|
I gave up Smoking, Drinking and Sex. :
It was the most *horrifying* 20 minutes of my life!:
---------------------------------------------------'
---------
Visit the Linux Supertore Online: http://www.redcorp.com !
If you want to be deleted from the list, send a mail to
majordomo@rtfm.be with "unsubscribe linux-team" in the body.
Archive of the list: http://tania.be.linux.org/