class base
{
    public:
    int y;
    int z;
    int x;
    base():x(1){}
    void output()
    {
        printf("%d\n",x);
        printf("%p\n",(void *)this);
        printf("%d\n",this->x);
    }
};

class der :public base
{
    public:
    int x;
    der():x(2){}
};



int main(int argc,char **argv)
{
    base x;
    der  y;

    x.output();
    puts("\n");
    y.output();
printf("%p,%p,&p\n“,&x,&y,(base *)&y);
return 0; }

   output函数的两次的输出分别什么?

 

 

 

 

    C++继承的一点细节问题

    可见,子类在使用基类的函数时,传入的this指针要转为基类的this指针。想想也正常,类的非static成员函数都有一个隐式的形参用来接受this指针,而这个形参的类型就是定义该函数的类的指针类型。在继承的过程中函数定义又不会发生改变。

  总结:子类对象调用基类函数  等价于 基类直接调用。

 

   

相关文章:

  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2021-07-02
相关资源
相似解决方案