(Why) does an empty string have an address?

前端 未结 7 1472
执笔经年
执笔经年 2021-02-19 14:19

I guessed no, but this output of something like this shows it does

string s=\"\";
cout<<&s;

what is the point of having empty string

7条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 14:35

    An 'empty' string object is still an object - there may be more to its internal implementation than just the memory required to store the literal string itself. Besides that, most C-style strings (like the ones used in C++) are null-terminated, meaning even that "empty" string still uses one byte for the terminator.

提交回复
热议问题