#include <iostream>
using namespace std;
class complax
{
public:
complax(double r=0,double i=0)
:re(r),im(i)
{cout<<"create function with parms"<<endl;}
double real()const{return re;}
double imag()const{return im;}
private:
double re,im;
};
int main() {
complax c1();
cout<<"~~~~~~~~"<<endl;
complax c3;
cout<<"~!!!!!!!!!"<<endl;
complax* c2 = new complax();
return 0;
}
输出:
C:\Users\12431\CLionProjects\untitled\cmake-build-debug\untitled.exe
~~~~~~~~
create function with parms
~!!!!!!!!!
create function with parms
Process finished with exit code 0
?三种构造方式中,第一种方式不会使用显式构造函数(使用隐藏构造函数),其他两种方式都会使用,且当出现歧义构造函数时候,第一种方式不会报错,而其他两种方式会报错,如下:
#include <iostream>
using namespace std;
class complax
{
public:
complax(double r=0,double i=0)
:re(r),im(i)
{
cout<<"create function with parms"<<endl;
}
complax()
:re(0),im(0)
{
cout<<"不带参数的构造函数"<<endl;
}
double real()const{return re;}
double imag()const{return im;}
private:
double re,im;
};
int main() {
complax c1();
cout<<"~~~~~~~~"<<endl;
complax c3;
cout<<"~!!!!!!!!!"<<endl;
complax* c2 = new complax();
return 0;
}
报错信息为:
?
?
|