对象的初始化发生在对象第一次获得值的是时候(被定义的时候,刚刚被分配内存的时候)。赋值--对一个已经被初始化的对象再次赋值(改变现有的状态)的过程。
//----初始化,调用构造函数----
CObject oA;
//----初始化,调用拷贝构造函数,注意只调用拷贝构造函数----
//----不调用构造函数。自己理解,构造函数是用来初始化对----
//----象的,拷贝构造函数也是一种构造函数,也是对对象进----
//----行初始化的,那么当初始化的时候,调用了拷贝构造,----
//----自然不再需要调用构造了。----
CObject oB = oA;
//----初始化,调用拷贝构造函数,注意只调用拷贝构造函数----
CObjectCopy(CObject oObject)
{
return oObject;
}
{
}
voidmain()
{
{
}
//----赋值,调用operator=----
CObject oB;
oB = oA;
注意:当以传值的方式来传递函数的参数或返回值时,如果参数或返回值为类的对象,则这个时候产生新的类对象且调用拷贝构造函数来初始化对象。注意这个时候,构造函数不会被调用。如果你有一个类,这个类有一个static成员变量来保存类的对像的数目。你在类的构造函数的时候将数目加一,在析构的时候减一。那么这个时候就会出现问题,因为这个时候,新的对象被创了出来,可是并没有调用构造函数,而是调用拷贝构造函数来对类的对象进行初始化。如果你在自己定义的拷贝构造函数中没有将对象的数目加1或者你没有声明自己的构造函数,而使用由编译器默认产生的拷贝构造函数(BitCopy).那么对象的数目将不对正确,但这些对象析构的时候都会调用析构函数,所以会出现对象的数目为负的情况。可以在拷贝构造函数也将对象的数目加1。因为对象产生后,一定会去调用构造函数(拷贝构造函数,再次强调,拷贝构造函数也是构造函数)去初始化,所以可以保证对象数目的正确性。
B a = 2;
B b=B(3);
B c(4);
以上三个等价均调用构造函数B(int m)。
B d(c);
B e=d;
以上两个调用拷贝构造函数。
另外,如果用户定义了任何一个构造函数,编译器不再生成缺省的构造函数B().
如果需要,编译器也会提供缺省的拷贝构造函数。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class student
{
public:
student()
{
cout<<"默认构造函数"<<endl;
}
student(int m)
{
cout<<"隐式转换函数"<<endl;
}
student(const student&)
{
cout<<"复制构造函数"<<endl;
}
student &operator=(const student &)
{
cout<<"赋值操作符"<<endl;
return *this;
}
~student()
{
cout<<"析构函数"<<endl;
}
};
void fun1(student obj)
{
}
void fun2(student &obj)
{
}
student fun3(student obj)
#include<vector>
#include<string>
using namespace std;
class student
{
public:
student()
{
cout<<"默认构造函数"<<endl;
}
student(int m)
{
cout<<"隐式转换函数"<<endl;
}
student(const student&)
{
cout<<"复制构造函数"<<endl;
}
student &operator=(const student &)
{
cout<<"赋值操作符"<<endl;
return *this;
}
~student()
{
cout<<"析构函数"<<endl;
}
};
void fun1(student obj)
{
}
void fun2(student &obj)
{
}
student fun3(student obj)
{
//student obj;
return obj;
}
student fun4()
{
student obj(4);
return obj;
}
student obj1;
student fun5()
{
return obj1;
}
int main()
{
student i0[2];
i0[0] = 4;
i0[1] = student(5);
cout<<"-------------------------"<<endl;
student i2 = 3;
student i1 = student(3);
cout<<"-------------------------"<<endl;
cout<<"student a"<<endl;
student a;
cout<<"fun1(a);"<<endl;
fun1(a);
cout<<"fun2(a);"<<endl;
fun2(a);
cout<<"-------------------------"<<endl;
cout<<"student y=fun3(i1);"<<endl;
student y=fun3(i1);
cout<<"a=fun3(i1);"<<endl;
a=fun3(i1);
cout<<"fun3(i1);"<<endl;
fun3(i1);
cout<<"-------------------------"<<endl;
cout<<"student x=fun4();"<<endl;
//student obj;
return obj;
}
student fun4()
{
student obj(4);
return obj;
}
student obj1;
student fun5()
{
return obj1;
}
int main()
{
student i0[2];
i0[0] = 4;
i0[1] = student(5);
cout<<"-------------------------"<<endl;
student i2 = 3;
student i1 = student(3);
cout<<"-------------------------"<<endl;
cout<<"student a"<<endl;
student a;
cout<<"fun1(a);"<<endl;
fun1(a);
cout<<"fun2(a);"<<endl;
fun2(a);
cout<<"-------------------------"<<endl;
cout<<"student y=fun3(i1);"<<endl;
student y=fun3(i1);
cout<<"a=fun3(i1);"<<endl;
a=fun3(i1);
cout<<"fun3(i1);"<<endl;
fun3(i1);
cout<<"-------------------------"<<endl;
cout<<"student x=fun4();"<<endl;
student x=fun4();
cout<<"i1=fun4();"<<endl;
i1=fun4();
cout<<"fun4();"<<endl;
fun4();
cout<<"-------------------------"<<endl;
cout<<"student z=fun5();"<<endl;
student z=fun5();
cout<<"i1=fun5();"<<endl;
i1=fun5();
cout<<"fun5();"<<endl;
fun5();
cout<<"-------------------------"<<endl;
cout<<"student *p=new student;"<<endl;
student *p=new student;
cout<<"vector<student> even(3);"<<endl;
vector<student> even(3);
cout<<"delete p"<<endl;
delete p;
cout<<"student b=a;"<<endl;
student b=a;
cout<<"student c;c=a; "<<endl;
student c;
c=a;
//system("pause");
return 0;
}
http://blog.csdn.net/kevinzhangyang/article/details/6638441
cout<<"i1=fun4();"<<endl;
i1=fun4();
cout<<"fun4();"<<endl;
fun4();
cout<<"-------------------------"<<endl;
cout<<"student z=fun5();"<<endl;
student z=fun5();
cout<<"i1=fun5();"<<endl;
i1=fun5();
cout<<"fun5();"<<endl;
fun5();
cout<<"-------------------------"<<endl;
cout<<"student *p=new student;"<<endl;
student *p=new student;
cout<<"vector<student> even(3);"<<endl;
vector<student> even(3);
cout<<"delete p"<<endl;
delete p;
cout<<"student b=a;"<<endl;
student b=a;
cout<<"student c;c=a; "<<endl;
student c;
c=a;
//system("pause");
return 0;
}
http://blog.csdn.net/kevinzhangyang/article/details/6638441