C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Déplacer une fonction

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Michael
Guest





PostPosted: Fri Jun 17, 2005 5:15 pm    Post subject: Déplacer une fonction Reply with quote



Bonsoir à tous,

tout d'abord désolé pour le titre pas très inspiré :)

Soient les deux classes suivantes:

class Elemt
{
private:
public:
Elemt() {}
Elemt(int pos);

void Rename(const AnsiString & new_name);
};

class Liste
{
private:
public:
Elemt Sheet;

Liste();

Elemt& Add(const AnsiString & sheet_name);
Elemt& Move(const AnsiString & sheet_name, const AnsiString &
sheet_after);
};

Elemt& Liste::Add(const AnsiString & sheet_name)
{
//blabla
Sheet = Elemt(1);
Sheet.Rename(sheet_name);

return Sheet;
}

Elemt& Liste::Move(const AnsiString & sheet_name, const AnsiString &
sheet_after)
{
//blabla

return Sheet;
}

Là quand je veux déplacer un élément de la liste, je passe par une
fonction Move de Liste.

Liste->Add("sheet");
Liste->Move("sheet","toto");

Je voudrais pouvoir utiliser la syntaxe suivante:

Liste->Add("sheet")->Move("toto");

Donc ajouter cette fonction à Elemt:

Elemt& Elemt::Move(const AnsiString & sheet_after)
{
//blabla
return *this;
}

Le problème est que Elemt ne connait pas les autres éléments constituant
la liste...

Comment je peux résoudre ça?

Merci d'avance...
Back to top
Pierre Lairez
Guest





PostPosted: Sun Jun 19, 2005 11:51 am    Post subject: Re: Déplacer une fonction Reply with quote



Le Fri, 17 Jun 2005 19:15:17 +0200, Michael
<michael_delva.enlever (AT) hotmail (DOT) com> a écrit:


Quote:
Comment je peux résoudre ça?

Merci d'avance...



Le mieux est sans doute d'implémenter un design avec des itérateurs. Une
fonction Add renvois un itérateur, une fonction Move prend en paramètre
deux itérateurs, une fonction Find te renvois un itérateur, etc.

Avec des itérateurs, du doit pourvoir écrire ça:
List->Add(string)->Move(iterator);

Back to top
Andre Heinen
Guest





PostPosted: Mon Jun 20, 2005 11:08 am    Post subject: Re: Déplacer une fonction Reply with quote



On 17 Jun 2005 17:15:17 GMT, Michael
<michael_delva.enlever (AT) hotmail (DOT) com> wrote:
Quote:

Le problème est que Elemt ne connait pas les autres éléments constituant
la liste...

Comment je peux résoudre ça?

Avec un proxy:

#include <iostream>
#include <string>

class NonTrouve {}; // exception

class Liste;
class Element;

class Proxy {
friend class Liste;
Liste* pListe;
Element* pElement;
Proxy(Liste* pLs,
Element* pEl);
public:
void deplacer(const std::string& ou) const;
};

class Element { // tout est prive
friend class Liste;
std::string nom;
Element* suivant;
Element(const std::string& n,
Element* s);
};

class Liste {
Element tete;
public:
Liste();
Proxy ajouter(const std::string& nom);
void deplacer(const std::string& quoi,
const std::string& ou);
void deplacer(Element* pQuoi,
const std::string& ou);
void afficher() const;
};

int main() {
Liste ls;
ls.ajouter("pomme");
ls.ajouter("orange");
ls.ajouter("cerise");
ls.ajouter("noix").deplacer("orange");
ls.ajouter("ananas");
ls.ajouter("citron").deplacer("noix");
ls.afficher();
}

Proxy::Proxy(Liste* pLs,
Element* pEl)
: pListe(pLs), pElement(pEl) {
}

void Proxy::deplacer(const std::string& ou) const {
pListe->deplacer(pElement, ou);
}

Element::Element(const std::string& n,
Element* s)
: nom(n), suivant(s) {
}

Liste::Liste()
: tete("", 0) {
}

Proxy Liste::ajouter(const std::string& nom) {
Element* tmp = tete.suivant;
tete.suivant = new Element(nom, tmp);
return Proxy(this, &tete);
}

/**
* deplace l'element "quoi" juste avant l'element "ou"
**/
void Liste::deplacer(const std::string& quoi,
const std::string& ou) {
Element* i;
for (i = &tete ;
i->suivant != 0 && i->suivant->nom != quoi ;
i = i->suivant);
if (i->suivant == 0) throw NonTrouve();
deplacer(i, ou);
}

/**
* Deplace un element juste avant l'element "ou".
* Precondition: pQuoi pointe l'element juste
* avant l'element a deplacer.
**/
void Liste::deplacer(Element* pQuoi,
const std::string& ou) {
Element* tmp = pQuoi->suivant;
pQuoi->suivant = pQuoi->suivant->suivant;
Element* i;
for (i = &tete ;
i->suivant != 0 && i->suivant->nom != ou ;
i = i->suivant);
if (i->suivant == 0) throw NonTrouve();
// si cette exception est declenchee,
//la liste a ete endommagee
tmp->suivant = i->suivant;
i->suivant = tmp;
}

void Liste::afficher() const {
for (const Element* i = &tete ;
i->suivant != 0 ;
i = i->suivant) {
std::cout << i->suivant->nom << ' ';
}
std::cout << 'n';
}

--
André Heinen
Mon e-mail, encodé ROT13: n qbg urvara ng rhebcrnayvax qbg pbz
La FAQ: http://www.cmla.ens-cachan.fr/Utilisateurs/dosreis/C++/FAQ/

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.