Posted on 2003-03-26
Medium Priority
Last Modified: 2008-02-01
Hello Guys,
Can someone help me please!!!
How can I concatinate strings in c++

like in Visual Basic just simply use & 
            String1 = "This is String1"

            String = String1 & " This is String2"

            String will become "This is string1 This is String2"

how about in C++ what is the best Operator

2. and in MFC what is the use of "->" operator and how can I use it

Question by:perci
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1

Author Comment

ID: 8215858
please answer

Accepted Solution

bkrahmer earned 40 total points
ID: 8215877
1.  CString (MFC) and std::string (STL) both have + and += operators.  Just make sure your lhs is an object.  The rhs can be an object or a quoted literal.  

2. -> is the deference operator.  It dereferences a pointer.  You can use it with classes or structures.

typedef struct
  char a;
  int b;
} foo;
foo *fooPtr;
char x = fooPtr->a;


Expert Comment

ID: 8215882
the -> is used when dealing with pointers and has nothing more to do with MFC than in any other C++ application, in particular, when you have a pointer to a variable, and you want to call a function/method that is a part of that variable.  that variable, then, must have functions associated with it, and so will more than likely be a class.  you don't need it for strings.

for strings you have a couple of ways to go, but it depends on what kind of strings you are using.

for intance, you can create your own null-terminated strings with an array of char and then can create a concat operation yourself.  likewise, by including cstring.h you can use strcat(string1, string2);
but you must note that string1 and string2 or simply null-terminated, array based strings.

when dealing with the String class used within MFC, since you mentioned it, you can just use the + operator to concat strings together.  nifty!

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8215886
to clarify the -> it doesn't have to be to a method or function, it's just that is the usual case because most member variables of classes are protected or private and won't be accessible unless you are building an inherited class.


Expert Comment

ID: 8215903
and also note that -> is not the only dereference operator.  * can be used to dereference as well.  it all depends on the situation.


Expert Comment

ID: 8215937
#include <string.h>

int main() {
   char mystring[50];

   strcpy(mystring, "hello");
   printf("%s\n", mystring); // prints "hello" to console

   strcat(mystring, " world");
   printf("%s\n", mystring); // prints "hello world" to
                             // console.

   return 0;

to concatenate a string, use strcat().
about MFC's '->'... this does not only appear in MFC but in c/c++ too... this is use to refer to a member of the data structure.

let say you have a structure with the following members :

typedef struct _MY_STRUCT {
   int value;
   int count;

when you have a structure like the one above, you should be wondering how am i suppose to refer to variable "value", or variable "count" in coding. therefore, depending on how you declare your variable of that structure type to be, whether as a pointer to or a variable, you use the '->' or '.' to refer to them, respectively.

int main() {
   MY_STRUCT first, * second;

   first.value = 10;
   first.count = 0;

   second->value = 20;
   second->count = 0;

   return 0;

you should see that the above code illustrate to different approach to access the member of the data structure. one using the '.' and the other using '->'.
the reason is clear if you look at the variables declared in the first line of code of the main().
first is defined as a variable of type MY_STRUCT, while
second is defined as a POINTER to the variable of type MY_STRUCT.
as "second" is a pointer, therefore we use '->' to refer to the member value.

as the above is true for c/c++ data structure, it is also true for c++ class. but it might be lengthy to explain the latter. therefore if you were to swift from VB to C/C++, it is advisable to get yourself a tutorial on C/C++.
good luck.


Author Comment

ID: 8215938
can you give me some good example please of no. 1, of concatinating 2 variables.

Expert Comment

ID: 8216182
std::string s1, s2, s3;

s1 = "a";
s2 = "b";
s3 = s1 + s2;
s3 = s1;
s3 += s2;
s3 += "c";


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question