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 

cast operator

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





PostPosted: Tue Dec 02, 2003 5:07 pm    Post subject: cast operator Reply with quote



Hi,

ich habe eine Klasse rational, die Brüche implementiert. Um Instanzen
dieser Klasse z.B. nach double zu wandeln, könnte ich einen
cast-Operator schreiben. Leider hat das den Effekt, dass der Compiler
dann bei jeder erstbesten Gelegenheit ein cast durchführt. Ich möchte
jedoch, dass immer ein explizites static_cast<double> notwendig ist, so
dass

rational r;
double d1=static_cast<double>(r);

o.k. wäre, bei

rational r;
double d1=r;

der Compiler aber meckert. Wie bekomme ich sowas hin?

Heiko


--
-- Vielleicht muß man die Liebe gefühlt haben, um die Freundschaft
-- richtig zu erkennen. (Sébastien Chamfort)
-- Supercomputing in Magdeburg @ http://tina.nat.uni-magdeburg.de
-- Heiko Bauke @ http://www.uni-magdeburg.de/bauke

--
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
Kostka, Volkmar
Guest





PostPosted: Tue Dec 02, 2003 5:14 pm    Post subject: Re: cast operator Reply with quote




Hallo,

Heiko Bauke wrote:
Quote:
Hi,

ich habe eine Klasse rational, die Brüche implementiert. Um Instanzen
dieser Klasse z.B. nach double zu wandeln, könnte ich einen
cast-Operator schreiben. Leider hat das den Effekt, dass der Compiler
dann bei jeder erstbesten Gelegenheit ein cast durchführt. Ich möchte
jedoch, dass immer ein explizites static_cast<double> notwendig ist, so
dass

rational r;
double d1=static_cast<double>(r);

o.k. wäre, bei

rational r;
double d1=r;

der Compiler aber meckert. Wie bekomme ich sowas hin?


Jedenfalls nicht mit einem cast.
Mit dem cast-Operator sagst du dem Compiler, dass ueberall, wo ein
double erlaubt ist, auch ein rational moeglich ist.
Warum verwendest du nicht eine explizite Umwandelroutine?

MFG

Volkmar Kostka

Volkmar Kostka

--
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
Heiko Bauke
Guest





PostPosted: Tue Dec 02, 2003 5:24 pm    Post subject: Re: cast operator Reply with quote



Hi,

On Tue, 02 Dec 2003 18:14:11 +0100
"Kostka, Volkmar" <volkmarkostka (AT) t-online (DOT) de> wrote:

Quote:
Warum verwendest du nicht eine explizite Umwandelroutine?

weil sich der Datentyp dann noch weniger, wie ein eingebauter Datentyp
verhält. Templatefunktionen wie

template<typename T>
double foo(T x) {
return std::exp(1.0/static_cast<double>(x));
}

funktionieren dann nicht mehr. (Gut, das Beispiel ist etwas an den
Haaren herangezogen, ich hoffe, das Grundproblem wird trotzdem klar.)

int i(2);
rational r(1, 2);
foo(i); // o.k.
foo(r); // peng!


Heiko

--
-- Vielleicht muß man die Liebe gefühlt haben, um die Freundschaft
-- richtig zu erkennen. (Sébastien Chamfort)
-- Supercomputing in Magdeburg @ http://tina.nat.uni-magdeburg.de
-- Heiko Bauke @ http://www.uni-magdeburg.de/bauke

--
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
Pether Hubert
Guest





PostPosted: Tue Dec 02, 2003 5:27 pm    Post subject: Re: cast operator Reply with quote

Heiko Bauke <heiko.bauke (AT) physik (DOT) uni-magdeburg.de> writes:
Quote:
Warum verwendest du nicht eine explizite Umwandelroutine?
weil sich der Datentyp dann noch weniger, wie ein eingebauter
Datentyp verhält.
[...]
int i(2);
rational r(1, 2);
foo(i); // o.k.
foo(r); // peng!

Aber wenn der Compiler hier implizit casten können soll, warum soll er
es dann bei

double d = r;

nicht dürfen? Woher soll er wissen, wann er darf und wann nicht,
bzw. nach welchen Kriterien soll er das entscheiden?

Ciao,

Pether
--
Gib jemandem einen Fisch, und du ernährst ihn einen Tag. Lehre ihn zu
fischen, und Du verlierst einen Stammkunden. (Erwerbsregel 208)

--
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
Markus Schaaf
Guest





PostPosted: Tue Dec 02, 2003 5:31 pm    Post subject: Re: cast operator Reply with quote

"Heiko Bauke" <heiko.bauke (AT) physik (DOT) uni-magdeburg.de> schrieb:

Quote:
rational r;
double d1=static_cast<double>(r);

o.k. wäre, bei

rational r;
double d1=r;

der Compiler aber meckert. Wie bekomme ich sowas hin?

Was heißt "meckert"?

--
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
Kostka, Volkmar
Guest





PostPosted: Tue Dec 02, 2003 6:19 pm    Post subject: Re: cast operator Reply with quote


Hallo,

Markus Schaaf wrote:

Quote:
"Heiko Bauke" <heiko.bauke (AT) physik (DOT) uni-magdeburg.de> schrieb:


rational r;
double d1=static_cast<double>(r);

o.k. wäre, bei

rational r;
double d1=r;

der Compiler aber meckert. Wie bekomme ich sowas hin?


Was heißt "meckert"?


Der OP moechte, dass sich rational bei Funktionsaufrufen wie ein double
verhaelt, aber gleichzeitig nicht an ein double ohne explizite
Konvertierung zugewiesen werden kann.

Ich sehe da keine Loesung.

MFG

Volkmar Kostka

--
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: Wed Dec 03, 2003 8:04 am    Post subject: Re: cast operator Reply with quote

Hallo,

Pether Hubert wrote:
Quote:
Heiko Bauke <heiko.bauke (AT) physik (DOT) uni-magdeburg.de> writes:

Warum verwendest du nicht eine explizite Umwandelroutine?

weil sich der Datentyp dann noch weniger, wie ein eingebauter
Datentyp verhält.

[...]
nicht dürfen? Woher soll er wissen, wann er darf und wann nicht,
bzw. nach welchen Kriterien soll er das entscheiden?

Ich kann die Anforderung durchaus nachvollziehen. Es wäre zuweile
hilfreich, wenn man die "Export"-Operatoren ebenso wie Konstruktoren als
/explicit/ declarieren könnte. Der Kontext, in dem sie verwendet werden
ist schließlich i.W. der gleiche. Normalerweise lautet die Empfehlung
statt dessen (flexiblere) Konstruktoren in der Zielklasse zu verwenden,
aber diese Option hat man bei Basis-Typen nicht.
Ein ähnliches Problem haben Smart-Pointer-Klassen. Und das ist der
Grund, warum sie grundsätzlich keine direkte Umwandlung zum
entsprechenden C-Style-Pointer erlauben.
--
Marcel Müller

--
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
Kurt Stege
Guest





PostPosted: Wed Dec 03, 2003 6:46 pm    Post subject: Re: cast operator Reply with quote

On Tue, 02 Dec 2003 19:19:00 +0100, "Kostka, Volkmar"
<volkmarkostka (AT) t-online (DOT) de> wrote:

Quote:
Markus Schaaf wrote:

"Heiko Bauke" <heiko.bauke (AT) physik (DOT) uni-magdeburg.de> schrieb:


rational r;
double d1=static_cast<double>(r);

o.k. wäre, bei

rational r;
double d1=r;

der Compiler aber meckert. Wie bekomme ich sowas hin?


Was heißt "meckert"?


Der OP moechte, dass sich rational bei Funktionsaufrufen wie ein double
verhaelt, aber gleichzeitig nicht an ein double ohne explizite
Konvertierung zugewiesen werden kann.

Stimmt so wohl nicht ganz. Der OP möchte, hat er zumindest
geschrieben, daß der Funktionsaufruf eben nicht akzeptiert
wird, aber eine Template-Funktion, die ausdrücklich durch
ein static_cast bestellt, "bitte alles akzeptieren, was auch
nur ganz grob wie ein double aussieht", auch ein rational
annimmt.

Oder knapper ausgedrückt:
foo(r) soll nicht gehen, foo(static_cast<double>(r)) dagegen
schon.

Und nein, ich weiß auch nicht, wie das gehen sollte.

Gruß,
Kurt.

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