Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • c++ 继承

    post by onelong / 2012-12-22 17:24 Saturday [C++]

    c++代码:

     

    class Object

    {

    private:

    int num;

    public:

    Object()

    {

    num = 50;

    }

     

    int getNum()

    {

    return num;

    }

    }; 

     

     

    class Person:public Object

    {

    private:

    int num;

    public:

    Person()

    {

    num = 100;

    }

    };

     

    class Woman:public Person

    {

    private:

    int num;

    public:

    Woman()

    {

    num = 250;

    }

    };

     

    int main()

    {

    Woman *p = new Woman();

    p->getNum();

    return 0;

    }

    汇编代码:

     

    .arch armv4t

    .fpu softvfp

    .eabi_attribute 20, 1

    .eabi_attribute 21, 1

    .eabi_attribute 23, 3

    .eabi_attribute 24, 1

    .eabi_attribute 25, 1

    .eabi_attribute 26, 2

    .eabi_attribute 30, 6

    .eabi_attribute 18, 4

    .file "person.cpp"

    .section .text._ZN6ObjectC2Ev,"axG",%progbits,_ZN6ObjectC2Ev,comdat

    .align 2

    .weak _ZN6ObjectC2Ev

    .type _ZN6ObjectC2Ev, %function

    _ZN6ObjectC2Ev:

    .fnstart

    .LFB1:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    @ link register save eliminated.

    str fp, [sp, #-4]!

    .LCFI0:

    add fp, sp, #0

    .LCFI1:

    sub sp, sp, #12

    .LCFI2:

    str r0, [fp, #-8]

    ldr r3, [fp, #-8]

    mov r2, #50

    str r2, [r3, #0]

    ldr r3, [fp, #-8]

    mov r0, r3

    add sp, fp, #0

    ldmfd sp!, {fp}

    bx lr

    .LFE1:

    .cantunwind

    .fnend

    .size _ZN6ObjectC2Ev, .-_ZN6ObjectC2Ev

    .section .text._ZN6Object6getNumEv,"axG",%progbits,_ZN6Object6getNumEv,comdat

    .align 2

    .weak _ZN6Object6getNumEv

    .type _ZN6Object6getNumEv, %function

    _ZN6Object6getNumEv:

    .fnstart

    .LFB3:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    @ link register save eliminated.

    str fp, [sp, #-4]!

    .LCFI3:

    add fp, sp, #0

    .LCFI4:

    sub sp, sp, #12

    .LCFI5:

    str r0, [fp, #-8]

    ldr r3, [fp, #-8]

    ldr r3, [r3, #0]

    mov r0, r3

    add sp, fp, #0

    ldmfd sp!, {fp}

    bx lr

    .LFE3:

    .cantunwind

    .fnend

    .size _ZN6Object6getNumEv, .-_ZN6Object6getNumEv

    .section .text._ZN6PersonC2Ev,"axG",%progbits,_ZN6PersonC2Ev,comdat

    .align 2

    .weak _ZN6PersonC2Ev

    .type _ZN6PersonC2Ev, %function

    _ZN6PersonC2Ev:

    .fnstart

    .LFB5:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    stmfd sp!, {fp, lr}

    .LCFI6:

    add fp, sp, #4

    .LCFI7:

    sub sp, sp, #8

    .LCFI8:

    str r0, [fp, #-8]

    ldr r3, [fp, #-8]

    mov r0, r3

    bl _ZN6ObjectC2Ev

    ldr r3, [fp, #-8]

    mov r2, #100

    str r2, [r3, #4]

    ldr r3, [fp, #-8]

    mov r0, r3

    sub sp, fp, #4

    ldmfd sp!, {fp, lr}

    bx lr

    .LFE5:

    .cantunwind

    .fnend

    .size _ZN6PersonC2Ev, .-_ZN6PersonC2Ev

    .section .text._ZN5WomanC1Ev,"axG",%progbits,_ZN5WomanC1Ev,comdat

    .align 2

    .weak _ZN5WomanC1Ev

    .type _ZN5WomanC1Ev, %function

    _ZN5WomanC1Ev:

    .fnstart

    .LFB9:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    stmfd sp!, {fp, lr}

    .LCFI9:

    add fp, sp, #4

    .LCFI10:

    sub sp, sp, #8

    .LCFI11:

    str r0, [fp, #-8]

    ldr r3, [fp, #-8]

    mov r0, r3

    bl _ZN6PersonC2Ev

    ldr r3, [fp, #-8]

    mov r2, #250

    str r2, [r3, #8]

    ldr r3, [fp, #-8]

    mov r0, r3

    sub sp, fp, #4

    ldmfd sp!, {fp, lr}

    bx lr

    .LFE9:

    .cantunwind

    .fnend

    .size _ZN5WomanC1Ev, .-_ZN5WomanC1Ev

    .text

    .align 2

    .global main

    .type main, %function

    main:

    .fnstart

    .LFB10:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    stmfd sp!, {r4, fp, lr}

    .save {r4, fp, lr}

    .LCFI12:

    .setfp fp, sp, #8

    add fp, sp, #8

    .LCFI13:

    .pad #12

    sub sp, sp, #12

    .LCFI14:

    mov r0, #12

    bl _Znwj

    mov r3, r0

    mov r4, r3

    mov r3, r4

    mov r0, r3

    bl _ZN5WomanC1Ev

    str r4, [fp, #-16]

    ldr r3, [fp, #-16]

    mov r0, r3

    bl _ZN6Object6getNumEv //直接调用Object的getNum,而不是Woman的

    mov r3, #0

    .L13:

    mov r0, r3

    sub sp, fp, #8

    ldmfd sp!, {r4, fp, lr}

    bx lr

    .LFE10:

    .fnend

    .size main, .-main

    .ident "GCC: (ctng-1.6.1) 4.4.3"

    .section .note.GNU-stack,"",%progbits

    从汇编里面可以看出,编译器会把所谓的类展开,子类构造函数自动调用父类的构造函数,是由编译器完成。调用哪个基类的方法也是编译器完成。这是没有虚函数的情况下。如果父类没有实现构造函数的话,编译自己会添加。

    引用地址:
     

    我要评论