官术网_书友最值得收藏!

Identity

Identity is what differentiates one object from another. Even if we try to declare two physically indistinguishable objects, they will still have different names for their variables, that is, different identities:

Product book1;
book1.rating = 4;
book1.name = "Book";
Product book2;
book2.rating = 4;
book2.name = "Book";

The objects in the preceding example have the same state, but they differ by the names we refer to them by, that is, book1 and book2. Let's say we had the ability to somehow create objects with the same name, as shown in the following code:

Product prod;
Product prod; // won't compile, but still "what if?"

If this was the case, they would still have different addresses in memory:

Identity is a fundamental property of the object and is one of the reasons why we can't create empty objects, such as the following:

struct Empty {};

int main() {
Empty e;
std::cout << sizeof(e);
}

The preceding code will not output 0 as expected. The size of an empty object is not specified in the standard; compiler developers tend to allocate 1 byte for such objects, though you might encounter 4 or 8 as well. Two or more instances of Empty should have different addresses in memory, so the compiler must make sure objects will take up at least 1 byte of memory.

主站蜘蛛池模板: 谢通门县| 榆树市| 手游| 博湖县| 弋阳县| 双鸭山市| 额济纳旗| 宜川县| 高清| 三明市| 乌拉特前旗| 河西区| 肇源县| 滨海县| 鄂尔多斯市| 沙洋县| 长武县| 尼木县| 宜兰县| 家居| 本溪市| 库伦旗| 杂多县| 建宁县| 六盘水市| 荔波县| 甘孜| 齐齐哈尔市| 巍山| 永州市| 永平县| 抚顺县| 苍山县| 永嘉县| 广平县| 葵青区| 利川市| 河间市| 德惠市| 盐山县| 阳春市|