首页 > 代码库 > 类的引用和复制

类的引用和复制

#include<iostream>
using namespace std;
class base{
public:
 base(){}
 virtual void func(int i = 12)
 {
  cout<<"base "<<i<<endl;
 }
};

class Derived:public base{
public:
 Derived(){}
 virtual void func(int i = 22)
 {
  cout<<"Derived "<<i<<endl;
 }
};

int main()
{
 Derived d;
 base& b1 = d;
 base b2 = d;
 b1.func();
 b2.func();
 d.func();
 return 0;
}

输出: Derived 12

               base 12

               Derived 22

1.默认参数静态绑定

2.c++通过基类引用指针调用虚函数时,才发生动态绑定。

3.base& b1 = d; 把d地址赋给b1; base b2 = d;把d中base内容赋给b2;

类的引用和复制