(Why) does an empty string have an address?

前端 未结 7 1500
执笔经年
执笔经年 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条回答
  •  猫巷女王i
    2021-02-19 14:35

    s is a string object so it has an address. It has some internal data structures keeping track of the string. For example, current length of the string, current storage reserved for string, etc.

    More generally, the C++ standard requires all objects to have a nonzero size. This helps ensure that every object has a unique address.

    9 Classes

    Complete objects and member subobjects of class type shall have nonzero size.

提交回复
热议问题