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 

Was zur Hölle passiert hier?

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





PostPosted: Mon May 22, 2006 10:21 pm    Post subject: Was zur Hölle passiert hier? Reply with quote



Hallo!


#include <iostream>
using namespace std;

class Lock
{ Lock() { cout << "Lock()\n"; }
Lock(int) { cout << "Lock(int)\n"; }
~Lock() { cout << "~Lock()\n"; }
};

int main()
{ int I;
cout << "in main 1\n";
Lock(I);
Lock(smoerebroed); // das compiliert wirklich!
cout << "in main 2\n";
return 0;
}

---------
Ergebnis:

in main 1
Lock()
Lock()
in main 2
~Lock()
~Lock()


Ich nahme mal an das wieder mal irgendeine syntaktische Spitzfindigkeit
im Spiel ist, aber ich habe wirklich keine Idee, was Lock(I) anderes
sein sollte, als ein Konstruktoraufruf.


Marcel

--
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
Marcel Müller
Guest





PostPosted: Mon May 22, 2006 10:21 pm    Post subject: Re: Was zur Hölle passiert hier? Reply with quote



Marcel Müller wrote:

Korrektur!

Mir ist beim Abspecken des Programms ein semantischer Fehler
unterlaufen. Das angegebene Programm compiliert nicht, und die
Fehlermeldung deutet schon auf die Ursache der eigentlichen Frage hin.
Offenbar ist "Lock(I);" die Definition einer Variable "I" vom Typ Lock.
Woher diese Syntax-Interpretation kommt, drängt sich mir allerdings
nicht auf. Schließlich darf ich ja auch "Lock(I).funktion();" schreiben.


Die Ursprüngliche Logik war so:

#include <iostream>
using namespace std;

class Lock
{ Lock() { cout << "Lock()\n"; }
Lock(int) { cout << "Lock(int)\n"; }
~Lock() { cout << "~Lock()\n"; }
};

int I;

int main()
{ cout << "in main 1\n";
Lock(I);
Lock(smoerebroed); // das compiliert wirklich!
cout << "in main 2\n";
return 0;
}


Marcel

--
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
Heinz Saathoff
Guest





PostPosted: Mon May 22, 2006 11:21 pm    Post subject: Re: Was zur Hölle passiert hier? Reply with quote



Moin,

Quote:
#include <iostream
using namespace std;

class Lock
{ Lock() { cout << "Lock()\n"; }
Lock(int) { cout << "Lock(int)\n"; }
~Lock() { cout << "~Lock()\n"; }
};

int main()
{ int I;
cout << "in main 1\n";
Lock(I);
Error: Lock::Lock of class Lock is not accessible


Quote:
Lock(smoerebroed); // das compiliert wirklich!
Error: Lock::Lock of class Lock is not accessible

// wenn access nicht klappt, scheint meinem Compiler der Rest
// nicht mehr zu interessieren

Quote:
cout << "in main 2\n";
return 0;
}


Wenn allerdings ein public spendiert wird, geht's auch bei mir durch den
Compiler.
Ich hatte zuerst an eine 'implicit int' Funktionsdeklaration gedacht,
also dass der Compiler
Lock(smoerebroed);
als
int Lock(int smoerebroed);
sieht.
Dass paßt aber nicht zu den ausgegebenen Meldungen des
Konstruktors/Destruktors.
Interessanterweise kommt ja auch 2 mal der Standardkonstruktor zum
Aufruf und nicht der Konversionskonstruktor.

Bin deshalb genauso verwirrt wie Du.


- Heinz

--
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
Thomas J. Gritzan
Guest





PostPosted: Tue May 23, 2006 12:22 am    Post subject: Re: Was zur Hölle passiert hier? Reply with quote

Marcel Müller schrieb:
Quote:
#include <iostream
using namespace std;

class Lock
{ Lock() { cout << "Lock()\n"; }
Lock(int) { cout << "Lock(int)\n"; }
~Lock() { cout << "~Lock()\n"; }
};

int main()
{ int I;
cout << "in main 1\n";
Lock(I);
Lock(smoerebroed); // das compiliert wirklich!

Du deklarierst zwei Variablen: I und smoerebroed.

Quote:
cout << "in main 2\n";
return 0;
}

---------
Ergebnis:

in main 1
Lock()
Lock()

Und die Variablen benutzen den Konstruktor ohne Parameter.

Quote:
in main 2
~Lock()
~Lock()


Ich nahme mal an das wieder mal irgendeine syntaktische Spitzfindigkeit
im Spiel ist, aber ich habe wirklich keine Idee, was Lock(I) anderes
sein sollte, als ein Konstruktoraufruf.

Was mich stört, ist, dass der Compiler nicht meckert, dass I zweimal
deklariert wird...
Mein Compiler meckert nämlich an zwei Stellen: Die Konstruktoren sind
private, und I wird zweimal deklariert. Du solltest vllt deine
Codebeispiele vorher testen, und wenn dein Compiler nicht meckert, einen
anderen benutzen und den ersten in die Tonne kloppen.

Ach ja: Bezeichner nur aus Großbuchstaben sind eigentlich für MAKROS
reserviert.

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
Jens Müller
Guest





PostPosted: Tue May 23, 2006 12:22 am    Post subject: Re: Was zur Hölle passiert hier? Reply with quote

Marcel Müller schrieb:
Quote:
Hallo!


#include <iostream
using namespace std;

class Lock
{ Lock() { cout << "Lock()\n"; }
Lock(int) { cout << "Lock(int)\n"; }
~Lock() { cout << "~Lock()\n"; }
};

int main()
{ int I;
cout << "in main 1\n";
Lock(I);
Lock(smoerebroed); // das compiliert wirklich!
cout << "in main 2\n";
return 0;
}


$ g++ test1.cpp
test1.cpp:5: Warnung: `class Lock' only defines a private destructor and
has no
friends
test1.cpp: In function `int main()':
test1.cpp:13: error: conflicting types for `Lock I'
test1.cpp:11: error: previous declaration as `int I'
test1.cpp:7: error: `Lock::~Lock()' is private
test1.cpp:13: error: within this context
test1.cpp:5: error: `Lock::Lock()' is private
test1.cpp:14: error: within this context
test1.cpp:7: error: `Lock::~Lock()' is private
test1.cpp:14: error: within this context


"public:" vor den Klassenrumpf:

$ g++ test1.cpp
test1.cpp: In function `int main()':
test1.cpp:15: error: conflicting types for `Lock I'
test1.cpp:13: error: previous declaration as `int I'


=> bei mir kompiliert da nix.

Ohne das "Lock(I)" geht es allerdings.

BTW: Lock(smoerebroed)(I); // das compiliert wirklich!

Also: Lock(smoerebroed) deklariert eine Variable vom Typ Lock, die
smoerebroed heißt. Ein paar überflüssige Klammern stören nicht.

Ein Konstruktur wird nicht aufgerufen (bei Dir, zwei Absätze weiter oben
schon). Siehe zB Seite 227 im Stroustrup, Special Edition. (§10.2.3).

Dein Compiler scheint btw reichlich kaputt zu sein.

--
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
Jens Müller
Guest





PostPosted: Tue May 23, 2006 12:22 am    Post subject: Re: Was zur Hölle passiert hier? Reply with quote

Marcel Müller schrieb:
Quote:
Marcel Müller wrote:

Korrektur!

Mir ist beim Abspecken des Programms ein semantischer Fehler
unterlaufen. Das angegebene Programm compiliert nicht, und die
Fehlermeldung deutet schon auf die Ursache der eigentlichen Frage hin.
Offenbar ist "Lock(I);" die Definition einer Variable "I" vom Typ Lock.
Woher diese Syntax-Interpretation kommt, drängt sich mir allerdings
nicht auf. Schließlich darf ich ja auch "Lock(I).funktion();" schreiben.


Stroustrup § 10.4.3:

An object can be created as: §10.4.10 A temporary object, which is
created as part of the evaluation of an expression and destroyed at the
end of the full expression in which it occurs.

§10.4.10:

"A temporary can also be created by explicitly invoking a constructor."

Das kann natürlich nur der Fall sein, wenn sowas als Teilausdruck
vorkommt. Sonst wäre der explizite Konstruktoraufruf syntaktisch
identisch mit einer Variablendefinition.

Nein, ich habe jetzt keine Lust, mir die Grammatik anzuschauen.

--
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
Jens Müller
Guest





PostPosted: Tue May 23, 2006 3:54 am    Post subject: Re: Was zur Hölle passiert hier? Reply with quote

Jens Müller schrieb:

Quote:
Ein Konstruktur wird nicht aufgerufen (bei Dir, zwei Absätze weiter oben
schon).

Also, nicht explizit "aufgerufen". Es wird natürlich der
Default-Konstruktor ausgeführt. Sorry, war vielleicht etwas mißverständlich.

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





PostPosted: Tue May 23, 2006 8:21 am    Post subject: Re: Was zur Hölle passiert hier? Reply with quote

Marcel Müller wrote:
Quote:
Marcel Müller wrote:

Mir ist beim Abspecken des Programms ein semantischer Fehler
unterlaufen. Das angegebene Programm compiliert nicht, und die
Fehlermeldung deutet schon auf die Ursache der eigentlichen
Frage hin. Offenbar ist "Lock(I);" die Definition einer
Variable "I" vom Typ Lock.

Freilich. So ist es auch. Es soll also ein Fehler sein, weil du
hast schon eine Variable namens I definiert.

Quote:
Woher diese Syntax-Interpretation kommt, drängt sich mir
allerdings nicht auf.

Wieso? Du darfst Klammern einsetzen, auch wenn die nicht nötig
sind, wie hier. Ohne Klammern hast du eindeutig eine Definition.
Die Klammern ändern nichts daran.

Quote:
Schließlich darf ich ja auch "Lock(I).funktion();" schreiben.

Freilich. Aber dann hast du eine Syntax, die *nicht* als
Declaration interpretiert werden kann. Die Regel ist, was als
Declaration interpretiert werden kann, wird als Declaration
interpretiert. Auch wenn es auch anders interpretiert werden
kann.

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

--
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
Heinz Ozwirk
Guest





PostPosted: Tue May 23, 2006 10:25 am    Post subject: Re: Was zur Hölle passiert hier? Reply with quote

"Marcel Müller" <news.5.maazl (AT) spamgourmet (DOT) org> schrieb im Newsbeitrag
news:4472342c$0$11062$9b4e6d93 (AT) newsread4 (DOT) arcor-online.net...
Quote:
Marcel Müller wrote:

Korrektur!

Mir ist beim Abspecken des Programms ein semantischer Fehler unterlaufen.
Das angegebene Programm compiliert nicht, und die Fehlermeldung deutet
schon auf die Ursache der eigentlichen Frage hin. Offenbar ist "Lock(I);"
die Definition einer Variable "I" vom Typ Lock.
Woher diese Syntax-Interpretation kommt, drängt sich mir allerdings nicht
auf. Schließlich darf ich ja auch "Lock(I).funktion();" schreiben.

Sicher, aber du darfst auch "Lock I;" schreiben oder "Lock (((((I)))));"
(aber nicht beides im gleichen Scope). Beides definiert eine Variable I vom
Typ Lock, allerdings mit einigen überflüssigen Klammern.

Heinz

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