您的位置:首页 > 编程语言 > C语言/C++

C++继承与构造函数一点说明

2015-09-10 20:16 267 查看
#include<iostream>
using namespace std;
int i=1;
class MyCls{
public:
MyCls():m_nFor(m_nThd),m_nSec(i++),m_nFir(i++),m_nThd(i++){
m_nThd=i;
}
void echo(){
cout<<"result:"<<m_nFir+m_nSec+m_nThd+m_nFor<<endl;
}
private:
int m_nFir;
int m_nSec;
int m_nThd;
int &m_nFor;
};
int main()
{
MyClsoCls;
oCls.echo();
return 0;
}
//结果为:11

析:如果为继承的话,此为[code]class
MyCls:BClass{},

[code]此中先对其初始化,顺序为private中的顺序,m_nFir、m_nSec、m_nThd、&m_nFor,

[code]先初始化MyCls():m_nFor(m_nThd),m_nSec(i++),m_nFir(i++),m_nThd(i++),之后初始化m_nThd=i;,此中先为m_nFir为1,之后m_nSec为2,m_nThd位3,之后m_nFor为引用类型,指向m_nThd,之后继续初始化m_nThd成为4,m_nFor指向m_nThd为4,故为1+2+4+4=11.

注:[code] m_nFor(m_nThd)等于m_nFor= m_nThd;m_nFir(i++)等于m_nFir=i++
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: