如果我传递派生类的对象,则应调用派生类catch块.但输出表明异常是由基类捕获的.为什么?
#include<iostream>
using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Derived d;
// some other stuff
try {
// Some monitored code
throw d;
}
catch(Base b) {
cout<<"Caught Base Exception";
}
catch(Derived d) { //This catch block is NEVER executed
cout<<"Caught Derived Exception";
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 代码正在打印所有构造函数.我读到当我们从另一个类派生一个类时,构造函数不会被继承.那么为什么创建c
是从b
和调用构造函数a
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public B, public A // Note the order
{
public:
C() { cout << "C's constructor called" << endl; }
};
int main()
{
C c;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在一个问题中,下面给出的第一个原型称为有效
int Function(int Tmp = Show());
Run Code Online (Sandbox Code Playgroud)
而在第二种情况下,它被称为无效声明.为什么?
float Function(int Tmp = Show(int, float));
Run Code Online (Sandbox Code Playgroud)
这是问题的链接 - http://www.indiabix.com/cpp-programming/functions/discussion-61