一个关于构造函数的调用问题
下面的程序我真是搞不清楚了。在创建对象ob1(1,2,3)时,它是怎么调用下面这三条语句的呢?
three_d(){x=y=z=0;}
three_d(int i,int j,int k) {x=i;y=j;z=k;}
three_d operator(){(int a,int b,int c);
创建ob2时又是怎么调用的呢?构造函数调用的是有参的还是无参的呢?同时它又是怎么调用operator()的???我就是弄不清楚,还望大家多多指教,谢谢!!!
#include <iostream>
using namespace std;
class three_d{
int x,y,z;
public:
three_d(){x=y=z=0;} // ????
three_d(int i,int j,int k) {x=i;y=j;z=k;}//?????
three_d operator(){(int a,int b,int c);
void show();};
three_d three_d::operator()(int a,int b,int c)
{
three_d temp;
temp.x=x+a;
temp.y=y+b;
temp.z=z+c;
return temp;
}
void three_d::show()
{
cout<<x<<" ,";
cout<<y<<" ,";
cout<<z<<" \n";
}
int main()
{
three_d ob1(1,2,3),ob2;//创建对象ob2时有没有调用three_d(){x=y=z=0;} ??
ob2=ob1(10,11,12);
cout<<"ob1:";
ob1.show();
cout<<"ob2:";
cout<<ob2.show();
return 0;
}
}
结果:ob1:1,2,3
ob2:11,13,15
using namespace std;
class three_d{
int x,y,z;
public:
three_d(){x=y=z=0;} // ????
three_d(int i,int j,int k) {x=i;y=j;z=k;}//?????
three_d operator(){(int a,int b,int c);
void show();};
three_d three_d::operator()(int a,int b,int c)
{
three_d temp;
temp.x=x+a;
temp.y=y+b;
temp.z=z+c;
return temp;
}
void three_d::show()
{
cout<<x<<" ,";
cout<<y<<" ,";
cout<<z<<" \n";
}
int main()
{
three_d ob1(1,2,3),ob2;//创建对象ob2时有没有调用three_d(){x=y=z=0;} ??
ob2=ob1(10,11,12);
cout<<"ob1:";
ob1.show();
cout<<"ob2:";
cout<<ob2.show();
return 0;
}
}
结果:ob1:1,2,3
ob2:11,13,15