Author Topic: A quick one - Concatenation operators + & - When to use them?  (Read 1679 times)

0 Members and 1 Guest are viewing this topic.

LE

  • Guest
A quick one - Concatenation operators + & - When to use them?
« on: November 16, 2006, 11:30:22 PM »
I am porting a code in VB to C++ and would like to know when or why a VB programmer use the concatenation operators + or & for the Strings...


Thanks!

LE

  • Guest
Re: A quick one - Concatenation operators + & - When to use them?
« Reply #1 on: November 16, 2006, 11:48:12 PM »
It is mostly curiosity, since I noticed that in the VB code they are using &

i.e:
Code: [Select]
S1 = sTotalSize & StrReverse(sPath) & sTotalSize

I end up using CString and did:
Code: [Select]
S1 = sTotalSize + sPath.MakeReverse() + sTotalSize;

Thanks!

Bryco

  • Water Moccasin
  • Posts: 1864
Re: A quick one - Concatenation operators + & - When to use them?
« Reply #2 on: November 16, 2006, 11:56:55 PM »
Good question, I've wondered that as well.   & looks cooler though

Dnereb

  • Guest
Re: A quick one - Concatenation operators + & - When to use them?
« Reply #3 on: November 17, 2006, 02:19:14 AM »
No it is usefull.

With the ampersant (&) you will glue to strings to gether without any mathematical process of adding or subtracting.
The + can and will do an addition in certain circumstances. The danger lies especcially in variants because they can hold strings as well as Numeric data.

Imagine a street with a sligtly odd numbering system (due to ancient locaL customs)
in this street (oddstreet) houses are nubered 1 to100 but a appartment buiolding was build later in the middle  with the numbers 50-1 to 50-80
It can happen you divide the numer and suffix in your database (pretty normal)
but if you retrieve those numbers into variants and put them together with the + houseumber 50 + -80 can become -30 because they were interpreted as two numbers to add up.

So the & is for adding string together and the + for adding numbers together.




 
« Last Edit: November 17, 2006, 02:22:24 AM by Dnereb »

Bryco

  • Water Moccasin
  • Posts: 1864
Re: A quick one - Concatenation operators + & - When to use them?
« Reply #4 on: November 17, 2006, 11:10:42 AM »
Nice, Peanuts in order Berend

LE

  • Guest
Re: A quick one - Concatenation operators + & - When to use them?
« Reply #5 on: November 17, 2006, 11:52:13 AM »
Thank you, it makes more sense now - !