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 

static array initialisieren

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





PostPosted: Wed Sep 29, 2004 8:52 am    Post subject: static array initialisieren Reply with quote



Hallo!
Ich habe eine Klasse mit einem statischen Array und statischen Variablen.

// XXX.H

class XXX
{
public:
char ident[32];
int a, b, c;
XXX();
XXX(char*, int, int, int);
static XXX* table[2];
static XXX AA, AB;
};

// XXX.cpp

XXX* XXX::table[2];
XXX XXX::AA = XXX("AA",1,2,3);
XXX XXX::AB = XXX("AB",4,5,6);

Und nun möchte ich den beiden Feldern des Array die Zeiger auf die
beiden statischen Klassenelemente AA und AB geben:

XXX* XXX::table[0] = &XXX::AA;
XXX* XXX::table[1] = &XXX::AB;

geht ja so nicht.
Wie bekommt man das hin, ohne es bei jeder Instanziierung im Konstruktor
zu machen?

Danke für Hilfe, Jan.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Jan Boehme
Guest





PostPosted: Wed Sep 29, 2004 12:47 pm    Post subject: Re: static array initialisieren Reply with quote



Jan Boehme schrieb:

Quote:
Wie bekommt man das hin, ohne es bei jeder Instanziierung im Konstruktor
zu machen?

Ich habe es mit Hilfe einer Initialisierungsliste hinbekommen.
Bin gespannt, ob es noch eine andere Lösung gibt.

Grüße, Jan.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Nicolas Pavlidis
Guest





PostPosted: Wed Sep 29, 2004 1:14 pm    Post subject: Re: static array initialisieren Reply with quote



Jan Boehme <aiscape (AT) hotmail (DOT) com> writes:

Quote:
Hallo!
Ich habe eine Klasse mit einem statischen Array und statischen Variablen.

// XXX.H

class XXX
{
public:
char ident[32];
int a, b, c;
XXX();
XXX(char*, int, int, int);
static XXX* table[2];

Warum Pointer und [] ?

Quote:
static XXX AA, AB;
};

// XXX.cpp

XXX* XXX::table[2];
XXX XXX::AA = XXX("AA",1,2,3);
XXX XXX::AB = XXX("AB",4,5,6);

Und nun möchte ich den beiden Feldern des Array die Zeiger auf die
beiden statischen Klassenelemente AA und AB geben:

XXX* XXX::table[0] = &XXX::AA;
XXX* XXX::table[1] = &XXX::AB;

geht ja so nicht.

Weil du die Adressen von XXX::AA und XXX::AB zuweiszt, nicht die Werte
Wink.

Einfach so:

class Example
{
public:
static int my_array[2];
// .....
}

Example::my_array[1] = VALUE_ONE;
Example::my_array[2] = VALUE_TWO;


Quote:
Wie bekommt man das hin, ohne es bei jeder Instanziierung im
Konstruktor zu machen?

Im Konstructor geht das recht schwer, weil ja statische Members nix mit
den Objekten zu tun haben, sondern allen Objekten der klasse gemeinsam
sind.

HTH && LG
Nicolas
--
Quote:
Nicolas Pavlidis | Elvis Presly: | |__ |
Student of SE & KM | "Into the goto" | |__| |
[email]pavnic (AT) sbox (DOT) tugraz.at[/email] | ICQ #320057056 | |
-------------------University of Technology, Graz----------------|

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Oliver Fischer
Guest





PostPosted: Wed Sep 29, 2004 1:15 pm    Post subject: Re: static array initialisieren Reply with quote

Ich würde es mit einer Initialisierungsklasse versuchen:

class InitMe
{
public:
InitMe()
{
Code...
}
} instanceOfInitMe;

Die Idee dahinter ist, daß wenn die Variable in
Jan Boehme wrote:
Quote:
geht ja so nicht.
Wie bekommt man das hin, ohne es bei jeder Instanziierung im Konstruktor
zu machen?

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Oliver Fischer
Guest





PostPosted: Wed Sep 29, 2004 1:16 pm    Post subject: Re: static array initialisieren Reply with quote

Ich würde es mit einer Initialisierungsklasse versuchen:

class InitMe
{
public:
InitMe()
{
Code...
}
} instanceOfInitMe;

Die Idee dahinter ist, daß wenn die Variable instanceOfInitMe erzeugt
wird, auf jeden Fall ihr Konstruktor aufgerufen werden muß. Innerhalb
dieses Konstruktors kannst Du dann machen, was Du willst.

MfG

Oliver Fischer

Jan Boehme wrote:
Quote:
geht ja so nicht.
Wie bekommt man das hin, ohne es bei jeder Instanziierung im Konstruktor
zu machen?

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Sep 29, 2004 2:09 pm    Post subject: Re: static array initialisieren Reply with quote

Jan Boehme wrote:
Quote:

[snip]


Quote:
Und nun möchte ich den beiden Feldern des Array die Zeiger auf die
beiden statischen Klassenelemente AA und AB geben:

XXX* XXX::table[0] = &XXX::AA;
XXX* XXX::table[1] = &XXX::AB;

geht ja so nicht.
Wie bekommt man das hin, ohne es bei jeder Instanziierung im Konstruktor
zu machen?

So wie man Arrays halt initialisiert :-)

XXX XXX::AA = XXX("AA",1,2,3);
XXX XXX::AB = XXX("AB",4,5,6);

XXX* XXX::table[2] = { &XXX::AA, &XXX::AB };


--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) 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.