友元类

来源:互联网 发布:淘宝爆款挖掘机 编辑:程序博客网 时间:2024/05/14 04:14
class TV
{
public:
    friend class Remote;                     ----1,
    void setTVvalue(){state = 0;}
private:
    int state;
    int volume;
    int maxchannel;
};
class Remote
{
private:
    int mode;
public:
    void changeTVvalue(TV & t){t.state = 1;}  // if no 1,../Chapt9.cpp:18:6: error: ‘int TV::state’ is private../Chapt9.cpp:27:31: error: within this context
};
int main()

{

    Remote a;
    TV b;
    a.changeTVvalue(b);

    return 0;

}


gcc -S Chapt9.c  //error 

gcc -S test.c        //

gcc -S Chapt9.cpp -lstdc++  //ok

g++ -S Chapt9.cpp

    .file    "Chapt9.cpp"
    .section    .text._ZN6Remote13changeTVvalueER2TV,"axG",@progbits,_ZN6Remote13changeTVvalueER2TV,comdat
    .align 2
    .weak    _ZN6Remote13changeTVvalueER2TV
    .type    _ZN6Remote13changeTVvalueER2TV, @function
_ZN6Remote13changeTVvalueER2TV:
.LFB1:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    movl    12(%ebp), %eax
    movl    $1, (%eax)
    popl    %ebp
    .cfi_def_cfa 4, 4
    .cfi_restore 5
    ret
    .cfi_endproc
.LFE1:
    .size    _ZN6Remote13changeTVvalueER2TV, .-_ZN6Remote13changeTVvalueER2TV
    .text
    .globl    main
    .type    main, @function
main:
.LFB2:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $32, %esp
    leal    16(%esp), %eax
    movl    %eax, 4(%esp)
    leal    28(%esp), %eax
    movl    %eax, (%esp)
    call    _ZN6Remote13changeTVvalueER2TV
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE2:
    .size    main, .-main
    .ident    "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section    .note.GNU-stack,"",@progbits



原创粉丝点击