Ziezi
Ziezi

Reputation: 6467

How member functions' additional syntax/specifiers affect memory layout in classes?

I think I have a clear understanding of class data members and their in-memory representation:

The members of a class define the layout of objects: data members are stored one after another in memory. When inheritance is used, the data members of the derived class are just added to those of a base.

However, when I am trying to figure out how the "blueprint" of an object is modified by its function members with additional syntax elements: I'm having difficulties. In the following text, I've tried to list all the problematic1 function member syntax that makes it difficult for me to figure out the object memory size and structure.

Class member functions that I couldn't figure out:

Question:

What are the differences between the member functions with different specifiers, in the context of object memory layout and how they affect it?


Note:

I've already read this and this, which does not provide an satisfying answer2. This talks about the general case(which I understand), which is the closest to a duplicate.(BUT I am particular about the list of problematic syntax that is my actual question and is not covered there.)

1. In terms of affecting object memory layout.

2. The first is talking about the GCC compiler and the second provides a link to a book on @m@zon.

Upvotes: 3

Views: 272

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

Member functions are not part of an object's memory layout. The only thing attributable to member functions is a hidden reference to an implementation-defined structure used to perform dynamic dispatch, such as a virtual method table. This reference is added to your object only if it has at least one virtual member function, so objects of classes that do not have virtual functions are free from this overhead.

Going back to your specific question, the only modifier to a member function that has any effect on the object's memory layout is virtual*. Other modifiers have an effect of how the function itself is interpreted, but they do not change the memory layout of your object.

* override keyword also indicates the presence of a virtual member function in a base class, but it is optional; adding or removing it does not change memory layout of the object.

Upvotes: 4

Related Questions