Skip to main content
gamedev.net
🔒 Locked

Preferred conversion operator an int?

Started by Endurion 4 replies 450+ views
Original Post
Endurion
Endurion
Hi, i'm using a formatting print routine which gets ellipses as parameter. This means the routine does not know at runtime what types it will get passed. Now i have a class that holds a parameter (internally as string), but i overloaded conversion operators for int, float and std::string for convinience. Now if i call the print routine with that class as parameter, which conversion operator will be chosen?
CMisc::printf( "%06d", m_GameData.m_Score );
It seems i got lucky and it worked; so my guess in this case the compiler choses int as the "general" operator (just as it assumes unknown functions being int functionname( int ); ). Am i guessing correct? Did i just get very very lucky? [edit: Correction] It doesn't work, i looked wrong. But anyway, what happens? It doesn't crash either and the debugger didn't let me check inside the operator, so i don't know which conversion is actually used. [2nd edit: Dang] Dang. Of course none of the operators is called. The object is passed by value. Simple as that. Thanks all for reading, that's all folks! [edited by - Endurion on January 8, 2004 5:27:24 AM]
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Rule
Rule
Good that it works now. But why use the ... operator when you have the elegant (and type safe) << operator. It is really difficult to debug a program when it doesn''t display numbers the right way. For instance, if it displays a float as an integer or vice versa. The elipses operator doens''t provide a mechanism for checking for such errors. With << you could write something like

CMisc::getInstance() << m_GameData.m_Score;

Ofcourse assuming that CMisc is a singleton and derived from std::ostream or something.
Endurion
Endurion
Nah, CMisc is just a class with static methods.

I''m perfectly fine with the ... operator, i was just wondering in that case.

I don''t want to use streams; i know they''re typesafe, but i dislike the << "feature" as it''s not as easy to read and format output. That, of course, is just my opinion.

Don''t try that at home kids.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Rule
Rule
quote:
Original post by Endurion
I don''t want to use streams; i know they''re typesafe, but i dislike the << "feature" as it''s not as easy to read and format output. That, of course, is just my opinion.


I agree with you on that. A printf line reads just as a normal line, and you can give char * to printf and later fill in the variables. This allows you to create some standards strings so you only have to change one string to change them all.
Actually, the logger in my engine also uses plain writeln functions I experimented with streams and found them to be to complex for the task.
amag
amag
There''s always boost::format (which is type-safe). However I''m not really fond of it so I''ve written my own format. You can find it on my homepage: http://www.rotd.org/amag/code.html
George2
George2
Use a cast so the compilers knows what conversion operator to choose :


CMisc::Printf( "%06d", (int)m_GameData.m_Score );



[edited by - George2 on January 8, 2004 10:41:32 AM]
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.