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 

[debutant]initialisation d'une vairiable d'instance

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





PostPosted: Wed May 11, 2005 3:00 pm    Post subject: [debutant]initialisation d'une vairiable d'instance Reply with quote



bonsoir,

comment initialiser un tableau membre d'une classe.

ex : facon java

class maClass {

int tab[] = {12, 15, -2, 14, 0, -6};

.../...
};

de facon elegante.

--
Bruno Causse
http://perso.wanadoo.fr/othello
Back to top
fabrizio
Guest





PostPosted: Wed May 11, 2005 3:07 pm    Post subject: Re: [debutant]initialisation d'une vairiable d'instance Reply with quote



Bruno Causse wrote:
Quote:
bonsoir,

comment initialiser un tableau membre d'une classe.

ex : facon java
class maClass {
int tab[] = {12, 15, -2, 14, 0, -6};
};
de facon elegante.

Ben dans le constructeur de ladite classe.
Peut-être ai-je mal compris la question.

Fabrice

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed May 11, 2005 5:01 pm    Post subject: Re: [debutant]initialisation d'une vairiable d'instance Reply with quote



fabrizio wrote:
Quote:
Bruno Causse wrote:

comment initialiser un tableau membre d'une classe.

ex : facon java
class maClass {
int tab[] = {12, 15, -2, 14, 0, -6};
};
de facon elegante.

Ben dans le constructeur de ladite classe.

Même dans le constructeur, ce n'est pas vraiment simple et
élégant. On ne peut pas le faire au moyen des clauses
d'initialisation, comme pour tout autre membre.

En fait, dans ce cas-ci, c'est std::vector qui est plus
élégant. Mais même là, il faut déclarer un tableau à la C,
statique, avec les valeurs d'initialisation. Un peu du genre :

class MaClass
{
std::vector< int > tab ;
// ...
} ;


static int initTab[] = { 12, 15, -2, 14, 0, -6 } ;

MaClass::MaClass()
: tab( begin( initTab ), end( initTab ) )
// ...

(Avec les définitions habituelles de begin et d'end,
évidemment.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


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.