1

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:

  • function type: lambda, pointer to function, modifying, non-modifying.
  • containing additional syntax elements: friend(with non-member), virtual, final, override, static, const, volatile, mutable.

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.

Community
  • 1
  • 1
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • Member functions don't affect object layout. Access modifiers don't affect object layout. Static members aren't part of the object, they're part of the class. – Barmar Sep 26 '15 at 11:18
  • @Barmar great, what is the case with `friend`, `virtual`? If you be so kind to include them in an answer all together, I'll be more then happy to utilize the facilities of SO and up-vote/accept it :) – Ziezi Sep 26 '15 at 11:22
  • 1
    @simplicisveritatis `virtual` is the only modifier that may influence a classes memory layout. – πάντα ῥεῖ Sep 26 '15 at 11:27
  • Why do you think `friend` would have any effect on layout? It just affects which classes are allowed to refer to the member. – Barmar Sep 26 '15 at 11:36
  • @Barmar You are right! I don't know, in my mind this is additional information allowing class data members to be accessed by an external entity. How the object knows which functions can and can not access its private data? – Ziezi Sep 26 '15 at 11:41
  • 1
    The object doesn't know that. It's all handled at compile time. – Barmar Sep 26 '15 at 11:43
  • @Barmar I see, so using the keyword `friend` it's like including a function to a namespace or something like that? – Ziezi Sep 26 '15 at 11:46
  • 1
    Yes, they're very similar concepts. – Barmar Sep 26 '15 at 11:46
  • @Barmar so to conclude, the only two things affecting object memory layout are: `data members` and the keyword `virtual` (as pointed out by dasblinkenlight and πάντα ῥεῖ). There is nothing else that can affect it. – Ziezi Sep 26 '15 at 11:49
  • 1
    That's correct. C++ classes are basically like C structs. Everything except virtual member functions are handled in the compiler by either restricting where names can be referenced, and doing name mangling for member functions and overloads. – Barmar Sep 26 '15 at 11:53

1 Answers1

2

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Is there any other additional syntax (not included in my list) that may influence object memory layout (just want to be sure to cover any unknown to me specifiers)? Just now, I thought of `functors` as a class member type, for example. – Ziezi Sep 26 '15 at 11:34
  • 1
    @simplicisveritatis lambdas, functors, and function pointers are regular data members. The fact that you can call them, and even the fact that they can access data members of your class, does not make them member functions. They are data members, so they are placed in memory in the same manner as "regular" data members, such as `int`s and pointers. – Sergey Kalinichenko Sep 26 '15 at 11:37