码迷,mamicode.com
首页 > 其他好文 > 详细

类的引用和复制

时间:2014-09-21 09:27:10      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:io   os   strong   sp   on   c   amp   r   bs   

#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;

类的引用和复制

标签:io   os   strong   sp   on   c   amp   r   bs   

原文地址:http://blog.csdn.net/gavin0123/article/details/39448869

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!