Probably because the compiler thinks that inlining does not help, because cout too expensive compared to overhead function calls. If you replace it with something simpler, for example. so that he joins the member, he will be included. See below for a conclusion.
#include <iostream> using std::cout; using std::endl; class Base { public: void foo() { i = 1; } virtual void bar() { i = 2; } virtual ~Base() = default; int i = 0; }; class Child : public Base { public: void foo() { i = 3; } void bar() { i = 4; } }; int main() { Base* obj = new Child; obj->foo(); obj->bar(); std::cout << obj->i << std::endl; //delete obj; }
Assembly:
Base::bar(): movl $2, 8(%rdi) ret Child::bar(): movl $4, 8(%rdi) ret Base::~Base(): ret Child::~Child(): ret Child::~Child(): jmp operator delete(void*) Base::~Base(): jmp operator delete(void*) main: subq $8, %rsp movl $16, %edi call operator new(unsigned long) movl $4, %esi movl std::cout, %edi call std::basic_ostream<char, std::char_traits<char> >::operator<<(int) movq %rax, %rdi call std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) xorl %eax, %eax addq $8, %rsp ret subq $8, %rsp movl std::__ioinit, %edi call std::ios_base::Init::Init() movl $__dso_handle, %edx movl std::__ioinit, %esi movl std::ios_base::Init::~Init(), %edi addq $8, %rsp jmp __cxa_atexit
source share