Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Simulating new ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 11 Topic 9498 of 9593
Post > Topic >>

Simulating new user-defined operators

by Vidar Hasfjord <vattilah-groups@[EMAIL PROTECTED] > Apr 14, 2008 at 12:43 PM

In C++ you cannot declare new user-defined operators, i.e. it is
impossible to define a new operator token with a specified precedent
and meaning, e.g. the euro symbol (EURO) to convert a value into euro
cents.

It is possible to simulate this to some degree using an object of a
user-defined class with an operator overload. For example, with an
"operator" object named E with a operator % overload, we can write (E%
v) to mean some operation on v.

I've found great utility for this idiom in two areas: error code
checking and resource string loading (internationalisation). For
example, with E% as a error-check operator and SL% as a string load
operator I can write:

   E%DeleteFile (file); // throws on error

and

   string caption = SL%IDS_VALIDATION_ERROR; // i18n
   string message = format (SL%IDS_OUT_OF_RANGE) % value;
   MessageBox (message, caption);

While I think such operator use may be considered obfuscation in the
general case, I think it has merits when it forms an idiom used in
large parts of the code. But would this code pass your code review?
Opinions welcome.

Regards,
Vidar Hasfjord

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 11 Posts in Topic:
Simulating new user-defined operators
Vidar Hasfjord <vattil  2008-04-14 12:43:32 
Re: Simulating new user-defined operators
Michael Aaron Safyan <  2008-04-14 21:25:12 
Re: Simulating new user-defined operators
"Martin T." <  2008-04-15 12:28:37 
Re: Simulating new user-defined operators
Edward Rosten <Edward.  2008-04-15 12:33:04 
Re: Simulating new user-defined operators
Alberto Ganesh Barbati &l  2008-04-15 23:19:14 
Re: Simulating new user-defined operators
Seungbeom Kim <musiphi  2008-04-15 23:18:03 
Re: Simulating new user-defined operators
ManicQin <ManicQin@[EM  2008-04-16 03:58:47 
Re: Simulating new user-defined operators
Vidar Hasfjord <vattil  2008-04-16 03:55:06 
Re: Simulating new user-defined operators
Carl Barron <cbarron41  2008-04-16 03:59:56 
Re: Simulating new user-defined operators
Michael Aaron Safyan <  2008-04-16 04:13:19 
Re: Simulating new user-defined operators
Vidar Hasfjord <vattil  2008-04-16 04:13:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 11:08:56 CDT 2008.