Can C ++ variables vary in size?
I will say yes and no. s will be the same string instance, but the internal buffer (which is preallocated depending on your STL implementation) will contain a copy of the constant string that you want to influence it. If the constant string (or any other char * or string) is larger than the internal preallocated buffer s, the s buffer will be reallocated depending on the string buffer reallocation algorithm implemented in your STL implantation.
source to share
This will lead to a dangerous discussion because the concept of "size" is not defined in your question.
The size of the class s is known at compile time, it's just the sum of the sizes of its members + any additional information needs to be kept for the classes (I admit I don't know all the details). it is important to get out of this, however, sizeof (s) will NOT change between assignments.
HOWEVER, s memory size can change at runtime using heap allocations. Since you are assigning a large string to s, its memory size will increase because more heap allocated space is likely to be required. You should probably try and specify what you want.
source to share
And exactly, exactly. The s variable refers to a string object.
#include <string>
using namespace std;
int main()
{
string s = "small"; //s is assigned a reference to a new string object containing "small"
s = "bigger"; //s is modified using an overloaded operator
}
Change, correct some details and clarify the point
See: http://www.cplusplus.com/reference/string/string/ and in particular http://www.cplusplus.com/reference/string/string/operator=/
The assignment causes the original content to be discarded and the content on the right side of the operation is copied into the object. similar to doing s.assign ("greater than"), but assignment has a wider range of parameters.
To get the original question, the contents of the s object can be of variable size. See http://www.cplusplus.com/reference/string/string/resize/ for details .
source to share
A variable is an object that we refer to by name. The "physical" size of the object - sizeof (s) in this case - never changes. They are still std :: string, and the size of std :: string is always constant. However, things like strings and vectors (and other containers, for that matter) have a "boolean size" that tells us how many elements of some type they hold. The string "logically" stores characters. I say "logically" because the string object contains no actual characters. It usually only has a couple of pointers as "physical members". Since string objects manage a dynamically allocated array of characters and provide correct copy semantics and easy access to characters, we can use these characters as members ("gates "). Since growing a row is a matter of memory reallocation and update pointers, we don't even need to resize (s).
source to share