I am C++ beginner. I have a code as below
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
char sym[] = "Audi.Despo";
//string sym ("Audi.Despo");
string rs(sym);
//cout << rs << endl;
rs = string(sym,4);
cout << rs;
return 0;
}
If the variable sym is a char array the final output of the string variable is
Audi
but if the same variable sym is a string the final output is .Despo (the suffix is printed)
Whats the explanation.
Looks like the line
rs = string(sym,4);
changes its behaviour if its input is a char [] or a string.
That’s just the different implementations of two of the constructor overloads: