C++ in class-declaration member initialization (last update: 2013-06-12, created: 2013-06-12) back to the list ↑
Notes from talking with a friend about C++, and member initialization in a class declaration.

In C++98 you could:
- Initialize a static const member. That's it.

C++11 is more permissive on this. See this document.

A simple test-code:


class A {
 public:
 int a = 5;
};

int func() {
 A x;
 return x.a;
}



Compiler: GCC g++ 4.6.3


> g++ asdf.cpp -std=c++0x
asdf.cpp:3:11: sorry, unimplemented: non-static data member initializers
asdf.cpp:3:11: error: ISO C++ forbids in-class initialization of non-const static member ‘a’

Too old.

Compiler: GCC g++ 4.7.2


> g++ -std=c++11 ./asdf.cpp -c
> objdump -d asdf.o

asdf.o:     file format elf32-i386


Disassembly of section .text:

00000000 <_Z4funcv>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 10                sub    $0x10,%esp
   6:   c7 45 fc 05 00 00 00    movl   $0x5,-0x4(%ebp)
   d:   8b 45 fc                mov    -0x4(%ebp),%eax
  10:   c9                      leave  
  11:   c3                      ret    

Quite interesting - the "implicit constructor" was inlined in the code. I was expecting a function to be created for this, though this seems better (faster), but it might lead to a code bloat (unless the behavior changes if more fields are present, which would make sense).

Compiler: clang 3.2


> clang ./asdf.cpp -std=c++11 -c
> objdump -d asdf.o

asdf.o:     file format elf32-i386


Disassembly of section .text:

00000000 <_Z4funcv>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 18                sub    $0x18,%esp
   6:   8d 45 f8                lea    -0x8(%ebp),%eax
   9:   89 04 24                mov    %eax,(%esp)
   c:   e8 fc ff ff ff          call   d <_Z4funcv+0xd>
  11:   8b 45 f8                mov    -0x8(%ebp),%eax
  14:   83 c4 18                add    $0x18,%esp
  17:   5d                      pop    %ebp
  18:   c3                      ret    

Disassembly of section .text._ZN1AC1Ev:

00000000 <_ZN1AC1Ev>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 08                sub    $0x8,%esp
   6:   8b 45 08                mov    0x8(%ebp),%eax
   9:   89 45 fc                mov    %eax,-0x4(%ebp)
   c:   8b 45 fc                mov    -0x4(%ebp),%eax
   f:   89 04 24                mov    %eax,(%esp)
  12:   e8 fc ff ff ff          call   13 <_ZN1AC1Ev+0x13>
  17:   83 c4 08                add    $0x8,%esp
  1a:   5d                      pop    %ebp
  1b:   c3                      ret    

Disassembly of section .text._ZN1AC2Ev:

00000000 <_ZN1AC2Ev>:
   0:   50                      push   %eax
   1:   8b 44 24 08             mov    0x8(%esp),%eax
   5:   89 04 24                mov    %eax,(%esp)
   8:   8b 04 24                mov    (%esp),%eax
   b:   c7 00 05 00 00 00       movl   $0x5,(%eax)
  11:   58                      pop    %eax
  12:   c3                      ret    


This one created the implicit constructor I was expecting. Well, actually two constructors, one in the other. Interesting.

TODO: Visual C++?
【 design & art by Xa / Gynvael Coldwind 】 【 logo font (birdman regular) by utopiafonts / Dale Harris 】