views:

57

answers:

2

All functions return CString, this is a MFC code and must compile in 32 & 64 bits.

Currently I'm using

CString sURI = GetURL();
sURI += GetMethod();
sURI += "?";
sURI += GetParameters();

Exists any manner to do the same like:

CString sURI = GetURL() + GetMethod() + "?" + GetParameters();
+2  A: 

As long as all those functions return a CString object, then it should be fine to use the + operator for concatenation.

Otherwise use the CString _T(const char *) function to wrap your regular C strings and make them a CString.

Luca Matteis
A: 

If you don't want to use an operator, use strcat

http://www.cplusplus.com/reference/clibrary/cstring/strcat/

/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}
glowcoder
strcat is dangerous if you don't ensure that your str buffer is bigger than the combinations of all the strings (plus 1 char for the null terminal byte). Especially when you are combining parameters from a web request like the OP does in the example. This is an easy source of buffer overflow attacks. Don't use strcat!
A. Levy
Obviously the buffer would need to be precomputed. This causes you to do a `strlen` for each string, and add the results together (remembering the extra space for the null). This does increase the runtime to 2n as opposed to n.
glowcoder
You shouldn't use strcat, strcpy, strlen, etc at all, imo (there are safer alternatives in most tools). Even moreso when you have string classes at your disposal...
Cogwheel - Matthew Orlando