C++: What is the size of an object of an empty class?

后端 未结 16 1850
悲&欢浪女
悲&欢浪女 2020-11-22 11:54

I was wondering what could be the size of an object of an empty class. It surely could not be 0 bytes since it should be possible to reference and

16条回答
  •  无人共我
    2020-11-22 12:42

    Allocation of 1 byte for an empty class is compiler dependent. Compilers need to make sure objects reside in different memory locations and they need to allocate non zero memory size to an object. Listen to notes on this topic here: http://listenvoice.com/listenVoiceNote.aspx?id=27

    Even though compilers allocates non zero size to an empty class they also do optimizations when new classes are derived from empty classes. Listen about empty base optimization on ListenVoice's c++ programming interview questions.

提交回复
热议问题