关于析构函数的疑惑~
程序如下:
#include <iostream.h>
#include <string.h>
class B
{
public:
B(char *s,double n)
{
strcpy(name,s);
b=n;
cout<<"Constructor\n";
}
B()
{
cout<<"Default Constructor\n";
}
~B()
{
cout<<"Destructor "<<name<<endl;
}
void getb(char *s,double& n)
{
strcpy(s,name);
n=b;
}
private:
char name[80];
double b;
};
void main()
{
B *p;
double n;
char s[80];
p=new B[3];
p[0]=B("ma",4.8);
//p[1]=B("wang",3.6);
//p[2]=B("li",3.1);
//delete[] p;
}
//运行结果如下:
Default Constructor
Default Constructor
Default Constructor
Constructor
Destructor ma
//我的问题是:为什么在p[0]=B("ma",4.8);执行完成后,要调用一次析构函数呢?谢谢