- Expert C++
- Vardan Grigoryan Shunguang Wu
- 223字
- 2021-06-24 16:34:00
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.
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- Learning Scala Programming
- Java多線程編程實戰指南:設計模式篇(第2版)
- Mobile Application Development:JavaScript Frameworks
- Visual Basic 6.0程序設計計算機組裝與維修
- Mastering Objectoriented Python
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- INSTANT Sencha Touch
- Learning FuelPHP for Effective PHP Development
- Learning Hadoop 2
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- ASP.NET Web API Security Essentials
- Sitecore Cookbook for Developers
- PHP動態網站開發實踐教程
- 現代C++語言核心特性解析