Home > Back-end >  Please help to analyze, process of these two programs
Please help to analyze, process of these two programs

Time:10-12


The include & lt; iostream>
# include & lt; stdio.h>
using namespace std;

The class Cexample
{
Private:
int a;
Public:
Cexample (int b)
{
A=b;
Printf (" constructor is callec \ n ");
}
Cexample (const Cexample & amp; C)
{
A=c.a.;
Printf (" the copy constructor is called \ n ");
}
~ Cexample ()
{
cout }
Void the show ()
{
cout }
};
Cexample g_fun (Cexample c)
{
cout <"G_func" & lt; }

Int main ()
{
Cexample a. (100);
Cexample B=A;
B.s how ();
G_fun (A);
return 0;
}
Results:
The constructor is callec
The copy constructor is called
100
The copy constructor is called
G_func
The destructor is called
The destructor is called
The destructor is called
The destructor is called
Only three original destructor is called, I run why there are 4, doubt?

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include & lt; iostream>
# include & lt; stdio.h>
using namespace std;

The class Cexample
{
Private:
int a;
Public:
Cexample (int b)
{
A=b;
Printf (" constructor is callec \ n ");
}
Cexample (const Cexample & amp; C)
{
A=c.a.;
Printf (" the copy constructor is called \ n ");
}
~ Cexample ()
{
cout }
Void the show ()
{
cout }
};
Cexample g_fun ()
{
Cexample temp (0);
return temp;
}

Int main ()
{
G_fun ();
return 0;
}
Results:
The constructor is callec
The destructor is called
The original run results:
The constructor is callec
The copy constructor is called
The destructor is called
The destructor is called
How to and the original text:
https://www.cnblogs.com/alantu2018/p/8459250.html
Results are very different, please help to have a look at! Thank you very much!
  • Related