引言:
#include<iostream>
using namespace std;
class Student
{
public:
Student()
{
cout<<"Student"<<endl;
}
private:
string m_strName;
} ;
int main()
{
Student stu1;
Student stu2=stu1;
Student stu3(stu1);
}
按照这一种代码,进行stu2和stu3进行初始化的时候,我们发现我们的程序只输出了一个“Student”,按照我们之前学习过的知识,初始化一个类型成员的时候一定要调用构造函数,问题出现在哪呢?
问题出现在:我们这样子调用的是拷贝构造函数而没有调用Student类里面的默认构造函数
拷贝构造函数的名称和类名是一样的,但是参数和默认构造函数有所区别
在这里,就应该这样子写
Student(const Student&stu){ };
这样子我们就写好了我们的拷贝构造函数
为了区别,我们在里面写上
cout<<"copy Student"<<endl;
在这里我试了一下const Student&stu和Student const&stu都可以过编译
总结:
1、当我们没有自己定义一个默认构造函数和拷贝构造函数的时候我们的编译器就会自己帮我们弄一个默认构造函数、拷贝构造函数
但是当我们定义了一个这样子的东西之后我们的编译器就不会帮我们弄了,因为我们已经有了我们自己的默认构造函数和拷贝构造函数。
2、很关键的一点是,构造函数分成两个大类,带参的构造函数和不带参的构造函数,其中不带参的构造函数和带的参数都有默认值的构造函数
统统叫做默认构造函数
3、初始化列表可以跟在构造函数后面,所有,构造函数后面
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student() :m_strName("Adison"), m_intnumber(20318)
{
cout << "Student" << endl;
}
Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
{
cout << "copy Student" << endl;
}
string printfName()
{
return m_strName;
}
int printfNum()
{
return m_intnumber;
}
private:
string m_strName;
int m_intnumber;
};
int main()
{
Student stu1;
cout << stu1.printfName() << endl;
cout << stu1.printfNum() << endl;
Student stu2 = stu1;
cout << stu2.printfName() << endl;
cout << stu2.printfNum() << endl;
Student stu3(stu1);
cout << stu3.printfName() << endl;
cout << stu3.printfNum() << endl;
system("pause");
}
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string a) :m_strName("Adison"), m_intnumber(20318)
{
setName2(a);
cout << "Student" << endl;
}
Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
{
setName();
cout << "copy Student" << endl;
}
string printfName()
{
return m_strName;
}
int printfNum()
{
return m_intnumber;
}
void setName()
{
m_strName="auto";
}
void setName2(string a)
{
m_strName=a;
}
private:
string m_strName;
int m_intnumber;
};
int main()
{
Student stu1("didi");
cout << stu1.printfName() << endl;
cout << stu1.printfNum() << endl;
Student stu2 = stu1;
cout << stu2.printfName() << endl;
cout << stu2.printfNum() << endl;
Student stu3(stu1);
cout << stu3.printfName() << endl;
cout << stu3.printfNum() << endl;
system("pause");
}
这里有两份我写的代码可以给大家玩一下hh~
但这里我只是自己有点不懂是不是所有构造函数都可以跟初始化列表,我试了一下发现是可以的~
另外要明确的一点是,我们的初始化列表执行顺序在构造函数的内容之前,所以说会有被覆盖的危险。
另外,我们如果调用了一个含Student类的参数的函数的时候,也会触发拷贝构造函数
例如下面这个例子
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string a) :m_strName("Adison"), m_intnumber(20318)
{
setName2(a);
cout << "Student" << endl;
}
Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
{
setName();
cout << "copy Student" << endl;
}
string printfName()
{
return m_strName;
}
int printfNum()
{
return m_intnumber;
}
void setName()
{
m_strName="auto";
}
void setName2(string a)
{
m_strName=a;
}
private:
string m_strName;
int m_intnumber;
};
void copy(Student a)
{
}
int main()
{
Student stu1("didi");
Student stu2 = stu1;
Student stu3(stu1);
copy(stu2);
system("pause");
}
PS:拷贝构造函数是不可重载的,是确定的
另外,从堆中构建一个类的对象,调用的是普通的构造函数,并不是调用拷贝构造函数
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string a) :m_strName("Adison"), m_intnumber(20318)
{
setName2(a);
cout << "Student" << endl;
}
Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
{
setName();
cout << "copy Student" << endl;
}
string printfName()
{
return m_strName;
}
int printfNum()
{
return m_intnumber;
}
void setName()
{
m_strName="auto";
}
void setName2(string a)
{
m_strName=a;
}
~Student()
{
cout<<"~Student"<<endl;
}
private:
string m_strName;
int m_intnumber;
};
void copy(Student a)
{
}
int main()
{
Student stu1("didi");
Student *stu2 = new Student("didi");
cout<<stu2->printfName()<<endl;
delete stu2;
system("pause");
}
另外,我还试了一下,发现“=”操作在初始化的时候是可以直接用的,但是在初始化之后用就会报错,我想这需要运算符重载了