If I have:
#include <string>
std::string myString = "Hello";
then do either:
myString = "Hello World";
or
myString+= " World";
Am I at risk of writing over some other memory?
Do I have to use string functions to add or change the size of the string?
There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand. compound assignment, in which an arithmetic, shift, or bitwise operation is performed before storing the result.
The assignment statement stores a value in a variable. Compound assignment combines assignment with another operator.
The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand, which must be a modifiable lvalue.
With std::string
, that's completely fine as std::string
manages its own memory.
With c-style strings using char*
, you need to manage your own memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With