Je ne comprends pas l'histoire d'opérateur d'entrée sortie.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question//mon Darray.h #include #include <string> using namespace std; class Darray { public: void ajouter(int valeur); //permet d'ajouter une case void operator=(Darray tab2); //on met tab2 dans tab1 void operator+(Darray tab1); //on concatène tab1 et tab2 int operator[](); bool estVide(); // vérifie que tab1 n'est pas vide private: int taille; //taille du tableau = nombre de case int tailleInterne; int* tab; };
#include "Darray.h" #include #include <string> using namespace std; Darray::Darray(int tailleTab1, int tailleIntTab1, int* tab1) //constructeur { this->taille = tailleTab1; this->tailleInterne = tailleIntTab1; this->tab = tab1; } void Darray::ajouter(int valeur) { } void Darray::operator=(Darray tab2) { if(this.taille()=tab2.taille()) { for(int i=0, i<this.taille(),++i) { } } } void Darray::operator+(Darray tab2) { } int Darray::operator[]() { } bool Darray::estVide() { if (taille=0) { return true; } else { return false; } }
bool Darray::estVide() { if (taille=0) { return true; } else { return false; } }
bool Darray::estVide() { return taille == 0; }
Pour définir mon opérateur d'accès [] j'ai besoin d'accéder à un élément de tab (cf le header) mais je ne trouve pas comment on fait cela X( "j'ai un peu honte"^^
Darray::Darray(int tailleTab, int tailleIntTab, int* tab) //constructeur { this->_taille = tailleTab; this->_tailleInterne = tailleIntTab; this->_tab = tab; }
this._taille()car taille est un élément privé. J'ai donc créé la méthode getTaille() :
int Darray::getTaille() { return this._taille(); }
- Passe tes objets par référence (&), sinon bonjour les coûteuse copie pour rien :). Ex: (Darray tab2) => (const Darray& tab2)
Par contre pour les std je vais veiller à utiliser des noms de fonctions corrects, mais j'aurai beaucoup de mal à m'en passer
Déja mon constructeur, on m'a dit qu'il m'en faudrait 2, je n'en vois qu'un
Darray::Darray() : _taille(0), _tailleInterne(32), _tab(new int[32]) { } Darray::Darray(int tailleTab, int* tab) : _taille(tailleTab), _tailleInterne(tailleTab + 32), _tab(new int[tailleTab + 32]) { strcnpy(_tab, tab, tailleTab + 32); }
taille est un élément privé
int Darray::getTaille() { return _taille; }
enfin dernier point qui me bloque vraiment beaucoup c'est le code de mon opérateur
int Darray::operator[](int indice)
{
return this._tab(indice);
}
Mais j'avoue ne pas bien saisir cette notion, donc si tu as un lien magique qui pourrait m'aider une fois de plus ca serait super.
void swap1(int a, int b) { int tmp = a; a = b; b = tmp; } void swap2(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } void swap3(int& a, int& b) { int tmp = a; a = b; b = tmp; } int main() { int a; int b; a = 56; b = 34; std::cout << a << " " << b << std::endl; swap1(a, b); // Ne fonctionne pas. a et b non inversé std::cout << a << " " << b << std::endl; a = 56; b = 34; swap2(&a, &b); // Fonctionne std::cout << a << " " << b << std::endl; a = 56; b = 34; swap3(a, b); // Fonctionne std::cout << a << " " << b << std::endl; return 0; }