C++调用顺序
2014-01-02 15:25
211 查看
1. 基类构造函数被调用的顺序以类派生表中声明的顺序为准.
class A
{
public:
A()
{
cout << "A::A() called" << endl;
num = 10;
}
void showMsg()
{
cout << "A::showMsg() called" << endl;
}
protected:
void showMsg2()
{
cout << "A::showMsg2() called" << endl;
}
private:
int num;
};
class B:private A
{
public:
B()
{
cout << "B::B() called" << endl;
}
void showMsgB()
{
cout << "B::showMsgB()" << endl;
}
void showMsgB2()
{
showMsg2();
}
};
class E
{
public:
E()
{
cout << "E::E()" << endl;
}
};
class F:public E, public B
{
public:
F()
{
cout << "F::F()" << endl;
}
};
...
int main(void)
{
F f;
return 0;
}
输出内容为:
2. 在含有基类、初始化成员函数的构造函数的调用顺序,基类最先调用->其次是成员初始化列表按照声明顺序进行调用->然后是成员类变量构造函数->本类的构造函数
class A
{
public:
A()
{
cout << "A::A() called" << endl;
num = 10;
}
void showMsg()
{
cout << "A::showMsg() called" << endl;
}
protected:
void showMsg2()
{
cout << "A::showMsg2() called" << endl;
}
private:
int num;
};
class B:private A
{
public:
B()
{
cout << "B::B() called" << endl;
}
void showMsgB()
{
cout << "B::showMsgB()" << endl;
}
void showMsgB2()
{
showMsg2();
}
};
class E
{
public:
E()
{
cout << "E::E()" << endl;
}
};
class F:public E, public B
{
public:
F()
{
cout << "F::F()" << endl;
}
};
...
int main(void)
{
F f;
return 0;
}
输出内容为:
E::E()A::A() calledB::B() calledF::F()
2. 在含有基类、初始化成员函数的构造函数的调用顺序,基类最先调用->其次是成员初始化列表按照声明顺序进行调用->然后是成员类变量构造函数->本类的构造函数
相关文章推荐
- 过滤敏感词,脏词
- C++面向对象类的实例题目十
- C++面向对象类的实例题目十
- android multithread in c/c++ to call JNI
- C++面向对象类的实例题目九
- C++面向对象类的实例题目九
- c++ 成员可见性
- C++模板学习
- C语言实现二进制文件读写
- C语言编码规范
- C++语言的15个晦涩特性
- [零基础学软件开发15]for循环语句
- C++内存管理
- [C++]C++小笔记之Simple File read and write-- ofstream and ifstream
- c++ 状态模式(state)
- C++ API函数
- 基于Visual C++6.0的声音文件操作
- 2024 C语言合法标识符
- C++中的模板特化
- C++中如何正确使用const