0

This is probably a very basic question for which I have been searching on google for the last 20 mins. I am not sure if i am phrasing it correctly, but I am not getting an explanation that I understand.

Basically, I have a string object and when I add an integer value x, it shortens the string by x characters.

Here is the code:

#include <iostream>
#include <string>

void Print::print(std::string str)
{
   std::cout << str << std::endl;    
}


print("formatString:" + 5);

The output is: tString:

Now i realise that the above is incorrect and during my search I have found ways correct the behaviour, but I haven’t found what is actually happening internally for me to get the above result.

Thanks

1
  • 1
    String literals are const char[N], not std::string. Commented Apr 8, 2014 at 23:02

4 Answers 4

2

The answer is simple: Pointer arithmetic.

Your string literal (array of const char including implicit 0-terminator), decays to a const char* on use, which you increment and pass to your print()-function, thus invoking the std::string-constructor for string literals.

So, yes, you start with a string object (0-terminated array of const char), but not a std::string object.

Sign up to request clarification or add additional context in comments.

Comments

1

Basically, I have a string object

No, you do not have a string object. "formatString:" is not a std::string, but a "string" literal. It is in fact a const char*. A const char* has a operator + defined that takes an integer and advances the value of the pointer with a number of positions. In your case it's 5.

To get a compiler error you'd have to wrap the literal in a std::string.

print(std::string("formatString:") + 5);

2 Comments

"To get a compiler error you'd have to wrap the literal in a std::string." - LOL
A string literal is actually a const char[] (C++11, §2.14.5/8). The array is just decaying to a pointer here.</pedantry>
0

"formatString:" is a string literal that has type const char[14] That is it is an array of const char with size equal to 14 (the array includes the terminating zero). In expressions like this

"formatString:" + 5

the array is implicitly converted to a pointer to its first element. So if for example const char *p denotes this pointer then the expression looks as

p + 5

The result of the expression is a pointer that points to the element of the array with index 5. That is there is used the pointer arithmetic.

P + 5 points to the first symbol of string "tString" And this expression is used by the constructor of class std::string.

Comments

0

Examine the following,

#include <iostream>

void print(std::string str)
{
    std::cout << str << std::endl;
}

int main(int argc, char* argv[])
{
    //following two lines created implicitly by the compiler
    const char* pstr = "formatString";
    std::string tmp(pstr + 5);  //string c-tor: string (const char* s);
    
    // now tmp: --> "tString"
    
    print(tmp);
    
    return 0;
}

pstr is a pointer and you are doing pointer arithmetic when you use + operation.

Note:Compiler may create different internal structure, but it is a instructive way to think the above two lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.