#include <iostream>
using namespace std;
class B
{
public:
B()
{
c++;
cout<<"B()called"<<endl;
}
B(int a)
{
c+=5;
cout<<"B(int)called"<<endl;
}
static int c;
int a;};
int B::c=0;
class D:public B
{
public:
D(int b)
{
//int h;
B(a);//貌似此处调用了默认构造函数???为什么呢?
c++;
}
int h;
};
void main()
{
D d(6);
cout<<d.c;
}
执行结果是:
B<>called
B<>called
3请按任意键进行...
一般都是在初始化列表中显示调用非默认基类构造函数的,但是貌似在函数体中也可以调用,但明明加了参数,确实调用的默认构造函数,为什么呢?
using namespace std;
class B
{
public:
B()
{
c++;
cout<<"B()called"<<endl;
}
B(int a)
{
c+=5;
cout<<"B(int)called"<<endl;
}
static int c;
int a;};
int B::c=0;
class D:public B
{
public:
D(int b)
{
//int h;
B(a);//貌似此处调用了默认构造函数???为什么呢?
c++;
}
int h;
};
void main()
{
D d(6);
cout<<d.c;
}
执行结果是:
B<>called
B<>called
3请按任意键进行...
一般都是在初始化列表中显示调用非默认基类构造函数的,但是貌似在函数体中也可以调用,但明明加了参数,确实调用的默认构造函数,为什么呢?
class D:public B
{
public:
D(int b)
{
//int h;
B(a);//貌似此处调用了默认构造函数???为什么呢?
B(5); // 这个调用的是B(int a)
c++;
}
int h;
};
B(5); // 这个调用的是B(int a)