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 

const wegcasten

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





PostPosted: Wed May 05, 2004 12:38 pm    Post subject: const wegcasten Reply with quote



Hallo!

Soviel wie ich weiss darf der Compiler bei

const int CONST1 = 2;

direkt CONST1 im Code durch 2 ersetzen.

Also nach:

int* pc = &CONST1;
*pc = 1;

nicht gewährleistet ist dass auch wirklich überall wo danach noch
CONST1 benutzt wird der Wert 2 oder 1 benutzt wird.

Ist das erstmal so korrekt?

Was ist mit Objecten? Zum Beispiel eine Klasse A:

class A{
public:
A(int i = 1) : i(i){};
int i;
};

const A a(4);
A* pa = &a;
pa->i = 4;

Ist das Verhalten definiert?

Kann's grad nicht probieren.

Gruss Thomas Thiele

--
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
Hendrik Belitz
Guest





PostPosted: Fri May 07, 2004 1:52 pm    Post subject: Re: const wegcasten Reply with quote



ThomasT wrote:

Quote:
Hallo!

Soviel wie ich weiss darf der Compiler bei

const int CONST1 = 2;

direkt CONST1 im Code durch 2 ersetzen.

Also nach:

int* pc = &CONST1;
*pc = 1;

Das wird nicht gehen, weil der Zeiger garnicht erst vom Compiler akzeptiert
werden würde. Richtig wäre

int const* pc = &CONST1;

und dann darf man den Inhalt von *pc auch nicht mehr ändern. Durch
const_cast<> kann man das alles wieder umgehen, aber das ist eine andere
Geschichte...
--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich

--
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
Martin Winkler
Guest





PostPosted: Fri May 07, 2004 5:06 pm    Post subject: Re: const wegcasten Reply with quote



Hendrik Belitz schrieb:

Quote:
int* pc = &CONST1;
*pc = 1;


Das wird nicht gehen, weil der Zeiger garnicht erst vom Compiler akzeptiert
werden würde. Richtig wäre

int const* pc = &CONST1;

und dann darf man den Inhalt von *pc auch nicht mehr ändern. Durch
const_cast<> kann man das alles wieder umgehen, aber das ist eine andere
Geschichte...

Das Wegcasten eines ursprünglich vorhandenen const führt aber zu
undefiniertem Verhalten.
Zulässig wäre es dann, wenn wir ursprünlich ein nicht-const hätten, das
"zwischendurch" als const betrachtet wurde. Dann darf man das const
wieder wegcasten.

Gruß
Martin

--
http://www.jumli.de
http://www.jumlidev.de/forum/

--
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
Stefan Reuther
Guest





PostPosted: Fri May 07, 2004 6:11 pm    Post subject: Re: const wegcasten Reply with quote

ThomasT wrote:
Quote:
Was ist mit Objecten? Zum Beispiel eine Klasse A:

class A{
public:
A(int i = 1) : i(i){};
int i;
};

const A a(4);
A* pa = &a;
pa->i = 4;

Ist das Verhalten definiert?

Du müsstest schon casten.
A* pa = const_cast<A*>(&a);
Damit sagst du dem Compiler, dass du jetzt ganz genau weisst, dass '&a'
eigentlich nicht const ist, nur dummerweise gerade den Typ 'const A*'
hat. "Wenn du den Compiler belügst, rächt er sich".

Alle Member eines Objektes vom Typ 'const A' sind automatisch ebenfalls
const. Ausnahmen sind mutable-Member. Aber ein public mutable-Member
dürfte in vielen Fällen auf ein Designproblem hinweisen.


Stefan

--
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
Rolf Magnus
Guest





PostPosted: Fri May 07, 2004 7:10 pm    Post subject: Re: const wegcasten Reply with quote

ThomasT wrote:

Quote:
Hallo!

Soviel wie ich weiss darf der Compiler bei

const int CONST1 = 2;

direkt CONST1 im Code durch 2 ersetzen.

Naja, er darf auf jeden Fall annehmen, daß sich der Wert _nie_ ändert
und basierend auf dieser Annahme beliebiege Optimierungen durchführen.

Quote:
Also nach:

int* pc = &CONST1;
*pc = 1;

geht nicht, da der Typ nicht passt. Du darfst &CONST1 nur einem
const int * zuweisen.

Quote:
nicht gewährleistet ist dass auch wirklich überall wo danach noch
CONST1 benutzt wird der Wert 2 oder 1 benutzt wird.

Ist das erstmal so korrekt?

Wenn man deine Zuweisung so abändert:

int* pc = const_cast<int*>(&CONST1);

dann ist das in etwa richtig, obwohl die C++-Norm hier generell von
undefiniertem Verhalten spricht. Das Programm darf also auch abstürzen
oder es darf irgendein anderer Wert nachher in der Variable stehen oder
eben irgendwas beliebiges anderes passieren.

Quote:
Was ist mit Objecten?

CONST1 ist auch ein Objekt. Wenn du Instanzen von Klassentypen meinst,
ist es auch dort nicht anders.

Quote:
Zum Beispiel eine Klasse A:

class A{
public:
A(int i = 1) : i(i){};
int i;
};

const A a(4);
A* pa = &a;
pa->i = 4;

Ist das Verhalten definiert?

Nein.

Quote:
Kann's grad nicht probieren.

Probieren bringt bei undefiniertem Verhalten auch nicht viel. Denn auch
Funktionieren des Code wie erwartet ist eine Instanz von undefiniertem
Verhalten.

--
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
ThomasT
Guest





PostPosted: Tue May 11, 2004 10:53 am    Post subject: Re: const wegcasten Reply with quote

Danke an alle.
Also unbestimmt. Das wollte ich wissen.

Quote:
int* pc = &CONST1;

Und ja. Ich hab den const_cast vergessen hinzuschreiben.
Haben aber einige gleich gemerkt.

Gruss Thomas

--
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.