2.2copy constructor的构建操作

来源:互联网 发布:vb do while 编辑:程序博客网 时间:2024/04/28 16:12

有三种情况,会以一个object的内容作为另一个class object的初值。

1.一个object做明确的初始化操作

class X;

X x;

X xx = x;

2.当object被当作参数交给某个函数时。

extern void foo(X x);

void bar()

{

    X xx;

    foo(xx);

}

3.当函数传回一个class object时。

X foo_bar()

{

    X xx;

    return xx;

}

 

Default Memberwise Initialization

当class object以“相同class的另一个object”作为初值时,其内部是以所谓的default memberwise initialization手法完成的,也就是把每一个内建的或派生的data member的值,从某个object拷贝一份到另一个object身上。不过它并不会拷贝其中的member class object,而是以递归的方式施行memberwise initialization。

“Default constructor和copy constructors在必要的时候才由编译器产生出来”

c++ standard 把copy constructor区分为trivial和nontrivial两种,只有nontrivial的实体才会被合成于程序之中。决定一个copy constructor是否为trivial的标准在于class是否展现出所谓的“bitwise copy semantics"

 

Bitwise Copy Semantics

 

举例:

 

class Word{

public:

  Word(const char*);

  ~Word(){delete[] str;}

private:

  int cnt;

  char* str;

};

Word::Word(const char*){

 

}

 

int main(){

  Word noun("book");

  Word verb = noun;

  return 0;

}

// 反汇编
objdump -dC test.o
test.o:     file format elf32-i386
Disassembly of section .text:
00000000 <Word::Word(char const*)>:
   0:55                   push   %ebp
   1:89 e5                mov    %esp,%ebp
   3:5d                   pop    %ebp
   4:c3                   ret    
00000005 <main>:
   5:55                   push   %ebp
   6:89 e5                mov    %esp,%ebp
   8:83 e4 f0             and    $0xfffffff0,%esp
   b:53                   push   %ebx
   c:83 ec 2c             sub    $0x2c,%esp
   f:c7 44 24 04 00 00 00 movl   $0x0,0x4(%esp)
  16:00 
  17:8d 44 24 18          lea    0x18(%esp),%eax
  1b:89 04 24             mov    %eax,(%esp)
  1e:e8 fc ff ff ff       call   1f <main+0x1a>
  23:8b 44 24 18          mov    0x18(%esp),%eax
  27:89 44 24 10          mov    %eax,0x10(%esp)
  2b:8b 44 24 1c          mov    0x1c(%esp),%eax
  2f:89 44 24 14          mov    %eax,0x14(%esp)
  33:bb 00 00 00 00       mov    $0x0,%ebx
  38:8d 44 24 10          lea    0x10(%esp),%eax
  3c:89 04 24             mov    %eax,(%esp)
  3f:e8 fc ff ff ff       call   40 <main+0x3b>
  44:8d 44 24 18          lea    0x18(%esp),%eax
  48:89 04 24             mov    %eax,(%esp)
  4b:e8 fc ff ff ff       call   4c <main+0x47>
  50:89 d8                mov    %ebx,%eax
  52:83 c4 2c             add    $0x2c,%esp
  55:5b                   pop    %ebx
  56:89 ec                mov    %ebp,%esp
  58:5d                   pop    %ebp
  59:c3                   ret    
Disassembly of section .text._ZN4WordD2Ev:
00000000 <Word::~Word()>:
   0:55                   push   %ebp
   1:89 e5                mov    %esp,%ebp
   3:83 ec 18             sub    $0x18,%esp
   6:8b 45 08             mov    0x8(%ebp),%eax
   9:8b 40 04             mov    0x4(%eax),%eax
   c:85 c0                test   %eax,%eax
   e:74 0e                je     1e <Word::~Word()+0x1e>
  10:8b 45 08             mov    0x8(%ebp),%eax
  13:8b 40 04             mov    0x4(%eax),%eax
  16:89 04 24             mov    %eax,(%esp)
  19:e8 fc ff ff ff       call   1a <Word::~Word()+0x1a>
  1e:c9                   leave  
  1f:c3                   ret    

 

不需要合成一个default copy constructor,因为上述声明展现了default copy semantics。

 

//举例

//test.cpp

#include<string>

using namespace std;

class Word{

public:

  Word(const string& s);

  ~Word(){}

private:

  int cnt;

  string str;

};

Word::Word(const string& s){

 

}

int main(){

  Word noun("book");

  Word verb = noun;

  return 0;

}

//objdump -dC test.o

编译器必须合成出一个copy constructor以便调用member class String object的copy constructor

test.o:     file format elf32-i386

 

 

Disassembly of section .text:

 

00000000 <Word::Word(std::string const&)>:

   0:55                   push   %ebp

   1:89 e5                mov    %esp,%ebp

   3:83 ec 18             sub    $0x18,%esp

   6:8b 45 08             mov    0x8(%ebp),%eax

   9:83 c0 04             add    $0x4,%eax

   c:89 04 24             mov    %eax,(%esp)

   f:e8 fc ff ff ff       call   10 <Word::Word(std::string const&)+0x10>

  14:c9                   leave  

  15:c3                   ret    

 

00000016 <main>:

  16:55                   push   %ebp

  17:89 e5                mov    %esp,%ebp

  19:83 e4 f0             and    $0xfffffff0,%esp

  1c:53                   push   %ebx

  1d:83 ec 3c             sub    $0x3c,%esp

  20:8d 44 24 2f          lea    0x2f(%esp),%eax

  24:89 04 24             mov    %eax,(%esp)

  27:e8 fc ff ff ff       call   28 <main+0x12>

  2c:8d 44 24 2f          lea    0x2f(%esp),%eax

  30:89 44 24 08          mov    %eax,0x8(%esp)

  34:c7 44 24 04 00 00 00 movl   $0x0,0x4(%esp)

  3b:00 

  3c:8d 44 24 28          lea    0x28(%esp),%eax

  40:89 04 24             mov    %eax,(%esp)

  43:e8 fc ff ff ff       call   44 <main+0x2e>

  48:8d 44 24 28          lea    0x28(%esp),%eax

  4c:89 44 24 04          mov    %eax,0x4(%esp)

  50:8d 44 24 20          lea    0x20(%esp),%eax

  54:89 04 24             mov    %eax,(%esp)

  57:e8 fc ff ff ff       call   58 <main+0x42>

  5c:8d 44 24 28          lea    0x28(%esp),%eax

  60:89 04 24             mov    %eax,(%esp)

  63:e8 fc ff ff ff       call   64 <main+0x4e>

  68:8d 44 24 2f          lea    0x2f(%esp),%eax

  6c:89 04 24             mov    %eax,(%esp)

  6f:e8 fc ff ff ff       call   70 <main+0x5a>

  74:8d 44 24 20          lea    0x20(%esp),%eax

  78:89 44 24 04          mov    %eax,0x4(%esp)

  7c:8d 44 24 18          lea    0x18(%esp),%eax

  80:89 04 24             mov    %eax,(%esp)

  83:e8 fc ff ff ff       call   84 <main+0x6e>

  88:bb 00 00 00 00       mov    $0x0,%ebx

  8d:8d 44 24 18          lea    0x18(%esp),%eax

  91:89 04 24             mov    %eax,(%esp)

  94:e8 fc ff ff ff       call   95 <main+0x7f>

  99:8d 44 24 20          lea    0x20(%esp),%eax

  9d:89 04 24             mov    %eax,(%esp)

  a0:e8 fc ff ff ff       call   a1 <main+0x8b>

  a5:89 d8                mov    %ebx,%eax

  a7:83 c4 3c             add    $0x3c,%esp

  aa:5b                   pop    %ebx

  ab:89 ec                mov    %ebp,%esp

  ad:5d                   pop    %ebp

  ae:c3                   ret    

  af:89 c3                mov    %eax,%ebx

  b1:8d 44 24 28          lea    0x28(%esp),%eax

  b5:89 04 24             mov    %eax,(%esp)

  b8:e8 fc ff ff ff       call   b9 <main+0xa3>

  bd:eb 12                jmp    d1 <main+0xbb>

  bf:89 c3                mov    %eax,%ebx

  c1:8d 44 24 20          lea    0x20(%esp),%eax

  c5:89 04 24             mov    %eax,(%esp)

  c8:e8 fc ff ff ff       call   c9 <main+0xb3>

  cd:eb 02                jmp    d1 <main+0xbb>

  cf:89 c3                mov    %eax,%ebx

  d1:8d 44 24 2f          lea    0x2f(%esp),%eax

  d5:89 04 24             mov    %eax,(%esp)

  d8:e8 fc ff ff ff       call   d9 <main+0xc3>

  dd:89 d8                mov    %ebx,%eax

  df:89 04 24             mov    %eax,(%esp)

  e2:e8 fc ff ff ff       call   e3 <main+0xcd>

  e7:89 c3                mov    %eax,%ebx

  e9:8d 44 24 20          lea    0x20(%esp),%eax

  ed:89 04 24             mov    %eax,(%esp)

  f0:e8 fc ff ff ff       call   f1 <main+0xdb>

  f5:89 d8                mov    %ebx,%eax

  f7:89 04 24             mov    %eax,(%esp)

  fa:e8 fc ff ff ff       call   fb <main+0xe5>

 

Disassembly of section .text._ZN4WordD2Ev:

 

00000000 <Word::~Word()>:

   0:55                   push   %ebp

   1:89 e5                mov    %esp,%ebp

   3:83 ec 18             sub    $0x18,%esp

   6:8b 45 08             mov    0x8(%ebp),%eax

   9:83 c0 04             add    $0x4,%eax

   c:89 04 24             mov    %eax,(%esp)

   f:e8 fc ff ff ff       call   10 <Word::~Word()+0x10>

  14:c9                   leave  

  15:c3                   ret    

 

Disassembly of section .text._ZN4WordC2ERKS_:

 

00000000 <Word::Word(Word const&)>:

   0:55                   push   %ebp

   1:89 e5                mov    %esp,%ebp

   3:83 ec 18             sub    $0x18,%esp

   6:8b 45 0c             mov    0xc(%ebp),%eax

   9:8b 10                mov    (%eax),%edx

   b:8b 45 08             mov    0x8(%ebp),%eax

   e:89 10                mov    %edx,(%eax)

  10:8b 45 0c             mov    0xc(%ebp),%eax

  13:8d 50 04             lea    0x4(%eax),%edx

  16:8b 45 08             mov    0x8(%ebp),%eax

  19:83 c0 04             add    $0x4,%eax

  1c:89 54 24 04          mov    %edx,0x4(%esp)

  20:89 04 24             mov    %eax,(%esp)

  23:e8 fc ff ff ff       call   24 <Word::Word(Word const&)+0x24>

  28:c9                   leave  

  29:c3                   ret    

---------------------------------------------------------------------------------------------------

不要Bitwise copy Semantics的四种情况:

1.当class内含一个member object而后者的class声明有一个copy constructor时

2.当class继承自一个base class 而后者存在有一个copy constructor时。

3.当class 声明了一个活多个virtual functions时。

4.当class派生自一个继承串链,其中有一个或多个virtual base classes时。

 

 

重新设定virtual table的指针

举例

//test.cpp

class ZooAnimal{

public:

  ZooAnimal();

  virtual ~ZooAnimal();

  virtual void animate();

  virtual void draw();

};

ZooAnimal::ZooAnimal(){}

ZooAnimal::~ZooAnimal(){}

void ZooAnimal::animate(){}

void ZooAnimal::draw(){}

class Bear:public ZooAnimal{

public:

  Bear();

  void animate();

  void draw();

  virtual void dance();

};

Bear::Bear(){}

void Bear::animate(){}

void Bear::draw(){}

void Bear::dance(){}

int main(){

  Bear yogi;

  Bear winnie = yogi;

  return 0;

}

 

反汇编

//objdump -dC test.o

08048504 <ZooAnimal::ZooAnimal()>:
 8048504:55                   push   %ebp
 8048505:89 e5                mov    %esp,%ebp
 8048507:8b 45 08             mov    0x8(%ebp),%eax
 804850a:c7 00 58 87 04 08    movl   $0x8048758,(%eax)
 8048510:5d                   pop    %ebp
 8048511:c3                   ret    
08048512 <ZooAnimal::~ZooAnimal()>:
 8048512:55                   push   %ebp
 8048513:89 e5                mov    %esp,%ebp
 8048515:83 ec 18             sub    $0x18,%esp
 8048518:8b 45 08             mov    0x8(%ebp),%eax
 804851b:c7 00 58 87 04 08    movl   $0x8048758,(%eax)
 8048521:b8 00 00 00 00       mov    $0x0,%eax
 8048526:83 e0 01             and    $0x1,%eax
 8048529:84 c0                test   %al,%al
 804852b:74 0b                je     8048538 <ZooAnimal::~ZooAnimal()+0x26>
 804852d:8b 45 08             mov    0x8(%ebp),%eax
 8048530:89 04 24             mov    %eax,(%esp)
 8048533:e8 f0 fe ff ff       call   8048428 <operator delete(void*)@plt>
 8048538:c9                   leave  
 8048539:c3                   ret    
0804853a <ZooAnimal::~ZooAnimal()>:
 804853a:55                   push   %ebp
 804853b:89 e5                mov    %esp,%ebp
 804853d:83 ec 18             sub    $0x18,%esp
 8048540:8b 45 08             mov    0x8(%ebp),%eax
 8048543:89 04 24             mov    %eax,(%esp)
 8048546:e8 c7 ff ff ff       call   8048512 <ZooAnimal::~ZooAnimal()>
 804854b:8b 45 08             mov    0x8(%ebp),%eax
 804854e:89 04 24             mov    %eax,(%esp)
 8048551:e8 d2 fe ff ff       call   8048428 <operator delete(void*)@plt>
 8048556:c9                   leave  
 8048557:c3                   ret    
08048558 <ZooAnimal::animate()>:
 8048558:55                   push   %ebp
 8048559:89 e5                mov    %esp,%ebp
 804855b:5d                   pop    %ebp
 804855c:c3                   ret    
 804855d:90                   nop
0804855e <ZooAnimal::draw()>:
 804855e:55                   push   %ebp
 804855f:89 e5                mov    %esp,%ebp
 8048561:5d                   pop    %ebp
 8048562:c3                   ret    
 8048563:90                   nop
08048564 <Bear::Bear()>:
 8048564:55                   push   %ebp
 8048565:89 e5                mov    %esp,%ebp
 8048567:83 ec 04             sub    $0x4,%esp
 804856a:8b 45 08             mov    0x8(%ebp),%eax
 804856d:89 04 24             mov    %eax,(%esp)
 8048570:e8 8f ff ff ff       call   8048504 <ZooAnimal::ZooAnimal()>
 8048575:8b 45 08             mov    0x8(%ebp),%eax
 8048578:c7 00 38 87 04 08    movl   $0x8048738,(%eax)
 804857e:c9                   leave  
 804857f:c3                   ret    
08048580 <Bear::animate()>:
 8048580:55                   push   %ebp
 8048581:89 e5                mov    %esp,%ebp
 8048583:5d                   pop    %ebp
 8048584:c3                   ret    
 8048585:90                   nop
08048586 <Bear::draw()>:
 8048586:55                   push   %ebp
 8048587:89 e5                mov    %esp,%ebp
 8048589:5d                   pop    %ebp
 804858a:c3                   ret    
 804858b:90                   nop
0804858c <Bear::dance()>:
 804858c:55                   push   %ebp
 804858d:89 e5                mov    %esp,%ebp
 804858f:5d                   pop    %ebp
 8048590:c3                   ret    
08048591 <main>:
 8048591:55                   push   %ebp
 8048592:89 e5                mov    %esp,%ebp
 8048594:83 e4 f0             and    $0xfffffff0,%esp
 8048597:53                   push   %ebx
 8048598:83 ec 2c             sub    $0x2c,%esp
 804859b:8d 44 24 1c          lea    0x1c(%esp),%eax
 804859f:89 04 24             mov    %eax,(%esp)
 80485a2:e8 bd ff ff ff       call   8048564 <Bear::Bear()>
 80485a7:8d 44 24 1c          lea    0x1c(%esp),%eax
 80485ab:89 44 24 04          mov    %eax,0x4(%esp)
 80485af:8d 44 24 18          lea    0x18(%esp),%eax
 80485b3:89 04 24             mov    %eax,(%esp)
 80485b6:e8 87 00 00 00       call   8048642 <Bear::Bear(Bear const&)>
 80485bb:bb 00 00 00 00       mov    $0x0,%ebx
 80485c0:8d 44 24 18          lea    0x18(%esp),%eax
 80485c4:89 04 24             mov    %eax,(%esp)
 80485c7:e8 16 00 00 00       call   80485e2 <Bear::~Bear()>
 80485cc:8d 44 24 1c          lea    0x1c(%esp),%eax
 80485d0:89 04 24             mov    %eax,(%esp)
 80485d3:e8 0a 00 00 00       call   80485e2 <Bear::~Bear()>
 80485d8:89 d8                mov    %ebx,%eax
 80485da:83 c4 2c             add    $0x2c,%esp
 80485dd:5b                   pop    %ebx
 80485de:89 ec                mov    %ebp,%esp
 80485e0:5d                   pop    %ebp
 80485e1:c3                   ret    
080485e2 <Bear::~Bear()>:
 80485e2:55                   push   %ebp
 80485e3:89 e5                mov    %esp,%ebp
 80485e5:83 ec 18             sub    $0x18,%esp
 80485e8:8b 45 08             mov    0x8(%ebp),%eax
 80485eb:c7 00 38 87 04 08    movl   $0x8048738,(%eax)
 80485f1:8b 45 08             mov    0x8(%ebp),%eax
 80485f4:89 04 24             mov    %eax,(%esp)
 80485f7:e8 16 ff ff ff       call   8048512 <ZooAnimal::~ZooAnimal()>
 80485fc:b8 00 00 00 00       mov    $0x0,%eax
 8048601:83 e0 01             and    $0x1,%eax
 8048604:84 c0                test   %al,%al
 8048606:74 0b                je     8048613 <Bear::~Bear()+0x31>
 8048608:8b 45 08             mov    0x8(%ebp),%eax
 804860b:89 04 24             mov    %eax,(%esp)
 804860e:e8 15 fe ff ff       call   8048428 <operator delete(void*)@plt>
 8048613:c9                   leave  
 8048614:c3                   ret    
 8048615:90                   nop
08048616 <Bear::~Bear()>:
 8048616:55                   push   %ebp
 8048617:89 e5                mov    %esp,%ebp
 8048619:83 ec 18             sub    $0x18,%esp
 804861c:8b 45 08             mov    0x8(%ebp),%eax
 804861f:89 04 24             mov    %eax,(%esp)
 8048622:e8 bb ff ff ff       call   80485e2 <Bear::~Bear()>
 8048627:8b 45 08             mov    0x8(%ebp),%eax
 804862a:89 04 24             mov    %eax,(%esp)
 804862d:e8 f6 fd ff ff       call   8048428 <operator delete(void*)@plt>
 8048632:c9                   leave  
 8048633:c3                   ret    
08048634 <ZooAnimal::ZooAnimal(ZooAnimal const&)>:
 8048634:55                   push   %ebp
 8048635:89 e5                mov    %esp,%ebp
 8048637:8b 45 08             mov    0x8(%ebp),%eax
 804863a:c7 00 58 87 04 08    movl   $0x8048758,(%eax)
 8048640:5d                   pop    %ebp
 8048641:c3                   ret    
08048642 <Bear::Bear(Bear const&)>:
 8048642:55                   push   %ebp
 8048643:89 e5                mov    %esp,%ebp
 8048645:83 ec 18             sub    $0x18,%esp
 8048648:8b 55 0c             mov    0xc(%ebp),%edx
 804864b:8b 45 08             mov    0x8(%ebp),%eax
 804864e:89 54 24 04          mov    %edx,0x4(%esp)
 8048652:89 04 24             mov    %eax,(%esp)
 8048655:e8 da ff ff ff       call   8048634 <ZooAnimal::ZooAnimal(ZooAnimal const&)>
 804865a:8b 45 08             mov    0x8(%ebp),%eax
 804865d:c7 00 38 87 04 08    movl   $0x8048738,(%eax)
 8048663:c9                   leave  
 8048664:c3                   ret    
 8048665:90                   nop
 8048666:90                   nop
 8048667:90                   nop
 8048668:90                   nop
 8048669:90                   nop
 804866a:90                   nop
 804866b:90                   nop
 804866c:90                   nop
 804866d:90                   nop
 804866e:90                   nop
 804866f:90                   nop

// 构建了 ZooAnimal和Bear的复制构造函数。

 

 

当发生切割时

举例

//test.cpp

#include"animal.h"

int main(){

  Bear yogi;

  ZooAnimal franny = yogi;

  return 0;

}

//animal.cpp
#include"animal.h"
ZooAnimal::ZooAnimal(){}
ZooAnimal::~ZooAnimal(){}
void ZooAnimal::animate(){}
void ZooAnimal::draw(){}
Bear::Bear(){}
void Bear::animate(){}
void Bear::draw(){}
void Bear::dance(){}
//animal.h
class ZooAnimal{
public:
  ZooAnimal();
  virtual ~ZooAnimal();
  virtual void animate();
  virtual void draw();
};
class Bear:public ZooAnimal{
public:
  Bear();
  void animate();
  void draw();
  virtual void dance();
};

 

//反汇编

objdump -dC test.o

test.o:     file format elf32-i386

 

 

Disassembly of section .text:

 

00000000 <main>:

   0:55                   push   %ebp

   1:89 e5                mov    %esp,%ebp

   3:83 e4 f0             and    $0xfffffff0,%esp

   6:53                   push   %ebx

   7:83 ec 2c             sub    $0x2c,%esp

   a:8d 44 24 1c          lea    0x1c(%esp),%eax

   e:89 04 24             mov    %eax,(%esp)

  11:e8 fc ff ff ff       call   12 <main+0x12>

  16:8d 44 24 1c          lea    0x1c(%esp),%eax

  1a:89 44 24 04          mov    %eax,0x4(%esp)

  1e:8d 44 24 18          lea    0x18(%esp),%eax

  22:89 04 24             mov    %eax,(%esp)

  25:e8 fc ff ff ff       call   26 <main+0x26>

  2a:bb 00 00 00 00       mov    $0x0,%ebx

  2f:8d 44 24 18          lea    0x18(%esp),%eax

  33:89 04 24             mov    %eax,(%esp)

  36:e8 fc ff ff ff       call   37 <main+0x37>

  3b:8d 44 24 1c          lea    0x1c(%esp),%eax

  3f:89 04 24             mov    %eax,(%esp)

  42:e8 fc ff ff ff       call   43 <main+0x43>

  47:89 d8                mov    %ebx,%eax

  49:83 c4 2c             add    $0x2c,%esp

  4c:5b                   pop    %ebx

  4d:89 ec                mov    %ebp,%esp

  4f:5d                   pop    %ebp

  50:c3                   ret    

  51:89 c3                mov    %eax,%ebx

  53:8d 44 24 1c          lea    0x1c(%esp),%eax

  57:89 04 24             mov    %eax,(%esp)

  5a:e8 fc ff ff ff       call   5b <main+0x5b>

  5f:89 d8                mov    %ebx,%eax

  61:89 04 24             mov    %eax,(%esp)

  64:e8 fc ff ff ff       call   65 <main+0x65>

 

Disassembly of section .text._ZN4BearD2Ev:

 

00000000 <Bear::~Bear()>:

   0:55                   push   %ebp

   1:89 e5                mov    %esp,%ebp

   3:83 ec 18             sub    $0x18,%esp

   6:8b 45 08             mov    0x8(%ebp),%eax

   9:c7 00 08 00 00 00    movl   $0x8,(%eax)

   f:8b 45 08             mov    0x8(%ebp),%eax

  12:89 04 24             mov    %eax,(%esp)

  15:e8 fc ff ff ff       call   16 <Bear::~Bear()+0x16>

  1a:b8 00 00 00 00       mov    $0x0,%eax

  1f:83 e0 01             and    $0x1,%eax

  22:84 c0                test   %al,%al

  24:74 0b                je     31 <Bear::~Bear()+0x31>

  26:8b 45 08             mov    0x8(%ebp),%eax

  29:89 04 24             mov    %eax,(%esp)

  2c:e8 fc ff ff ff       call   2d <Bear::~Bear()+0x2d>

  31:c9                   leave  

  32:c3                   ret    

 

Disassembly of section .text._ZN4BearD0Ev:

 

00000000 <Bear::~Bear()>:

   0:55                   push   %ebp

   1:89 e5                mov    %esp,%ebp

   3:83 ec 18             sub    $0x18,%esp

   6:8b 45 08             mov    0x8(%ebp),%eax

   9:89 04 24             mov    %eax,(%esp)

   c:e8 fc ff ff ff       call   d <Bear::~Bear()+0xd>

  11:8b 45 08             mov    0x8(%ebp),%eax

  14:89 04 24             mov    %eax,(%esp)

  17:e8 fc ff ff ff       call   18 <Bear::~Bear()+0x18>

  1c:c9                   leave  

  1d:c3                   ret    

 

Disassembly of section .text._ZN9ZooAnimalC2ERKS_:

 

00000000 <ZooAnimal::ZooAnimal(ZooAnimal const&)>:

   0:55                   push   %ebp

   1:89 e5                mov    %esp,%ebp

   3:8b 45 08             mov    0x8(%ebp),%eax

   6:c7 00 08 00 00 00    movl   $0x8,(%eax)

   c:5d                   pop    %ebp

   d:c3                   ret    

// 反汇编objdump -dC animal.o
animal.o:     file format elf32-i386
Disassembly of section .text:
00000000 <ZooAnimal::ZooAnimal()>:
   0:55                   push   %ebp
   1:89 e5                mov    %esp,%ebp
   3:8b 45 08             mov    0x8(%ebp),%eax
   6:c7 00 08 00 00 00    movl   $0x8,(%eax)
   c:5d                   pop    %ebp
   d:c3                   ret    
0000000e <ZooAnimal::~ZooAnimal()>:
   e:55                   push   %ebp
   f:89 e5                mov    %esp,%ebp
  11:83 ec 18             sub    $0x18,%esp
  14:8b 45 08             mov    0x8(%ebp),%eax
  17:c7 00 08 00 00 00    movl   $0x8,(%eax)
  1d:b8 00 00 00 00       mov    $0x0,%eax
  22:83 e0 01             and    $0x1,%eax
  25:84 c0                test   %al,%al
  27:74 0b                je     34 <ZooAnimal::~ZooAnimal()+0x26>
  29:8b 45 08             mov    0x8(%ebp),%eax
  2c:89 04 24             mov    %eax,(%esp)
  2f:e8 fc ff ff ff       call   30 <ZooAnimal::~ZooAnimal()+0x22>
  34:c9                   leave  
  35:c3                   ret    
00000036 <ZooAnimal::~ZooAnimal()>:
  36:55                   push   %ebp
  37:89 e5                mov    %esp,%ebp
  39:83 ec 18             sub    $0x18,%esp
  3c:8b 45 08             mov    0x8(%ebp),%eax
  3f:89 04 24             mov    %eax,(%esp)
  42:e8 fc ff ff ff       call   43 <ZooAnimal::~ZooAnimal()+0xd>
  47:8b 45 08             mov    0x8(%ebp),%eax
  4a:89 04 24             mov    %eax,(%esp)
  4d:e8 fc ff ff ff       call   4e <ZooAnimal::~ZooAnimal()+0x18>
  52:c9                   leave  
  53:c3                   ret    
00000054 <ZooAnimal::animate()>:
  54:55                   push   %ebp
  55:89 e5                mov    %esp,%ebp
  57:5d                   pop    %ebp
  58:c3                   ret    
  59:90                   nop
0000005a <ZooAnimal::draw()>:
  5a:55                   push   %ebp
  5b:89 e5                mov    %esp,%ebp
  5d:5d                   pop    %ebp
  5e:c3                   ret    
  5f:90                   nop
00000060 <Bear::Bear()>:
  60:55                   push   %ebp
  61:89 e5                mov    %esp,%ebp
  63:83 ec 04             sub    $0x4,%esp
  66:8b 45 08             mov    0x8(%ebp),%eax
  69:89 04 24             mov    %eax,(%esp)
  6c:e8 fc ff ff ff       call   6d <Bear::Bear()+0xd>
  71:8b 45 08             mov    0x8(%ebp),%eax
  74:c7 00 08 00 00 00    movl   $0x8,(%eax)
  7a:c9                   leave  
  7b:c3                   ret    
0000007c <Bear::animate()>:
  7c:55                   push   %ebp
  7d:89 e5                mov    %esp,%ebp
  7f:5d                   pop    %ebp
  80:c3                   ret    
  81:90                   nop
00000082 <Bear::draw()>:
  82:55                   push   %ebp
  83:89 e5                mov    %esp,%ebp
  85:5d                   pop    %ebp
  86:c3                   ret    
  87:90                   nop
00000088 <Bear::dance()>:
  88:55                   push   %ebp
  89:89 e5                mov    %esp,%ebp
  8b:5d                   pop    %ebp
  8c:c3                   ret    
Disassembly of section .text._ZN4BearD2Ev:
00000000 <Bear::~Bear()>:
   0:55                   push   %ebp
   1:89 e5                mov    %esp,%ebp
   3:83 ec 18             sub    $0x18,%esp
   6:8b 45 08             mov    0x8(%ebp),%eax
   9:c7 00 08 00 00 00    movl   $0x8,(%eax)
   f:8b 45 08             mov    0x8(%ebp),%eax
  12:89 04 24             mov    %eax,(%esp)
  15:e8 fc ff ff ff       call   16 <Bear::~Bear()+0x16>
  1a:b8 00 00 00 00       mov    $0x0,%eax
  1f:83 e0 01             and    $0x1,%eax
  22:84 c0                test   %al,%al
  24:74 0b                je     31 <Bear::~Bear()+0x31>
  26:8b 45 08             mov    0x8(%ebp),%eax
  29:89 04 24             mov    %eax,(%esp)
  2c:e8 fc ff ff ff       call   2d <Bear::~Bear()+0x2d>
  31:c9                   leave  
  32:c3                   ret    
Disassembly of section .text._ZN4BearD0Ev:
00000000 <Bear::~Bear()>:
   0:55                   push   %ebp
   1:89 e5                mov    %esp,%ebp
   3:83 ec 18             sub    $0x18,%esp
   6:8b 45 08             mov    0x8(%ebp),%eax
   9:89 04 24             mov    %eax,(%esp)
   c:e8 fc ff ff ff       call   d <Bear::~Bear()+0xd>
  11:8b 45 08             mov    0x8(%ebp),%eax
  14:89 04 24             mov    %eax,(%esp)
  17:e8 fc ff ff ff       call   18 <Bear::~Bear()+0x18>
  1c:c9                   leave  
  1d:c3                   ret    
//编译器只产生了ZooAnimal的copy constructor。
//编译器在需要constructor的每个目标文件中产生此constructor副本。
处理virtual base class subobject
一个class object如果以另一个object作为初值,而后者有一个virtual base class subobject,那么也会使"bitwise copy semantics"失效。
编译器,必须让“derived class object中的virtual base class subobject位置”在执行期准备妥当。“bitwise copy constructor"可能会破坏这个位置,所以编译器必须在它自己合成出来的copy constructor中做出裁决。

 

 

 

 

 

 

 

 

 

原创粉丝点击