Jump to content
  • Advertisement
Sign in to follow this  
Phantoms

Question about $ in C++

This topic is 4007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is $ used in C++ for anything? I remeber years ago string varibles in basic I was using requiring $. Would using it in C++ pose any downside? For instance, instead of:
std::string str = "what";

would doing it like this open me to any problems?
std::string str$ = "what";

I find that a variable name such as str$ instantly identifies that varible as a string type for me (maybe just an old habit).

Share this post


Link to post
Share on other sites
Advertisement
I seems like C++ accounts for '$' as another character, so it's legal to use is in variable names. Maybe you could use it to identify strings among other types, just like using a 'p' prefix for pointers or 'I' for interfaces. It's not mandatory, but it's possible to use it in order to give clarity and unity to your code.

Share this post


Link to post
Share on other sites
Thanks for the replies. This is what I planned to use it for, clarity. It just easily identifies that variable as a string type at first sight.

I just wanted to make sure I wasn't opening myself up to any unforeseen problems.

Share this post


Link to post
Share on other sites
Quote:
Original post by Phantoms
I just wanted to make sure I wasn't opening myself up to any unforeseen problems.


Visual Studio's compiler seems to permit $ in variable names, but Borland's BCC55 does not and bombs out with an illegal character error.

So I'd assume that permitting it is non-standard, so best avoided (unless it has been recently added to the standard, and no-one told me - BCC55 is a bit old now).

To my knowledge, a fully standards compilant symbol token in C++ must start with an alphabetic character or underscore and contain only alphanumeric characters or underscores.

Even if you were only ever planning to use a compiler that supports it, I'd still avoid it since if you ever need to post code here or anywhere else, or show to another C++ programmer, it is really only going to cause confusion.

Share this post


Link to post
Share on other sites
Sounds to me like you need to start using better variable names.

The disadvantages of hungarian notation and/or sigils, while disputed, are well known.

My own stance is terribly anti-systems-hungarian: If the type of a variable isn't 100% intuitively obvious from it's name/usage alone, then chances are good that the actual type will eventually change, thus making any and all type warting become horrible, horrible lies sometime in the (probably near) future.

Apps Hungarian can be useful in limited cases -- Wikipedia's example of "strName" under this category is absolute horrible bollocks that should die though, IMO, or properly refiled under Systems Hungarian (the "___ containing a __singular_noun__" is a bit of a giveaway, especially when the first blank is "a string", a type supported by anything worth calling a programming language, sans assembly languages).

Random quote from the earlier linked page from C++'s very author:
Quote:
Bjarne Stroustrup:

No I don't recommend "Hungarian". I regard "Hungarian" (embedding an abbreviated version of a type in a variable name) a technique that can be useful in untyped languages, but is completely unsuitable for a language that supports generic programming and object-oriented programming - both of which emphasize selection of operations based on the type an arguments (known to the language or to the run-time support). In this case, "building the type of an object into names" simply complicates and minimizes abstraction.

Share this post


Link to post
Share on other sites
If I remember correctly, the '%' postfix was used in Basic to declare integers, and that is definitely not going to work.

Share this post


Link to post
Share on other sites
As already explained, and as confirmed by MSDN, the dollar sign is not allowed as part of an identifier in C++, and Visual C++ supports it as a non-standard language extension.

As also already explained, altering the name of a variable to indicate its type is useless if the name of the variable already implies its type (iCount provides no additional information when compared to simply count) and arguably dangerous if it doesn't (as the type of the object may change even though its name stays the same).

Share this post


Link to post
Share on other sites
I'm using CodeBlocks and it allows it. I tried in VS2005E and it does too. However, for reasons pointed out here, I will continue my learning without adding $.

Thanks guys/gals.


-James

Share this post


Link to post
Share on other sites
A note for the curious: VC++, at least, uses the dollar sign to delimit the decoration on a name. So if you have an exported method that isn't extern "C" and you look at it, you'll have Identifier$Decoration.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!