小编sam*_*her的帖子

当我抛出派生类的对象时,为什么捕获基类的块正在捕获异常?

如果我传递派生类的对象,则应调用派生类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++ inheritance exception-handling

6
推荐指数
1
解决办法
653
查看次数

如果构造函数不是继承的话,为什么会被调用?

代码正在打印所有构造函数.我读到当我们从另一个类派生一个类时,构造函数不会被继承.那么为什么创建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)

c++ oop inheritance multiple-inheritance

4
推荐指数
1
解决办法
949
查看次数

为什么第一个原型是正确的而第二个是不正确的?

在一个问题中,下面给出的第一个原型称为有效

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

c++ function

1
推荐指数
1
解决办法
66
查看次数