如果没有提供任何构造函数,创建一个什么都不做的构造函数
如:
test::test()
{}
如果希望编译器不使用这种默认的构造函数
可以自己定义默认构造函数
test::test()
{
...........;
}
当然也可以自己传入值定义构造函数
test::test(const char* tin)
{
...........;
}
2、复制构造函数
它用于将一个对象复制到一个新创建的对象中,类的复制构造函数原型如下:
class_name(const class_name &);
关于复制构造函数下面几种情况会使用到:
test my1(my); 显示调用,my1是和my一样的副本
test my1 = test(my); 显示调用,my1是和my一样的副本
test my1 = my; 隐试调用,my1是和my一样的副本
void tf1(test t1);函数传值生成临时的类对象在函数中使用
test *p = new test(my);未测试
简单的说程序生成了对象的副本,编译器都会使用复制构造函数;既然是副本我们可以想象如果
类中定义的是指针类型,那么指针指向的是同一片位置。如果此时调用析构函数会重复的释放
同一片内存区域,同时如果不定义复制构造函数,那么某些计数器将不你能正常工作。
另外我们注意同一段函数中如果类中有静态对象如下:
private:
static int num_s;
使用
int charin::num_s = 0;
那么在调用这个类的函数中多次调用它使用值是共享的因为是同一块内存区域。这个其实和一般
的静态内部变量没什么两样
我们来看一段代码
点击(此处)折叠或打开
-
::::::::::::::
-
c2.h
-
::::::::::::::
-
/*************************************************************************
-
> File Name: c2.h
-
> Author: gaopeng
-
> Mail: gaopp_200217@163.com
-
> Created Time: Fri 24 Jun 2016 01:47:19 AM CST
-
************************************************************************/
-
-
#include<iostream>
-
using namespace std;
-
-
class charin
-
{
-
private:
-
static int num_s;
-
char* sin;
-
int len;
-
public:
-
charin(void);
-
charin(const char *);
-
//charin(charin &);
-
~charin(void);
-
friend std::ostream & operator<<(std::ostream &,const charin &);
-
};
-
::::::::::::::
-
cf.cpp
-
::::::::::::::
-
/*************************************************************************
-
> File Name: cf.cpp
-
> Author: gaopeng
-
> Mail: gaopp_200217@163.com
-
> Created Time: Fri 24 Jun 2016 01:59:13 AM CST
-
************************************************************************/
-
-
#include<iostream>
-
#include<string.h>
-
#include"c2.h"
-
using namespace std;
-
-
int charin::num_s = 0;
-
-
charin::charin(void)
-
{
-
len = strlen("study c++")+1;
-
cout << len <<" mem alloc!"<<endl;
-
sin = new char[len];
-
strcpy(sin,"study c++");
-
num_s++;
-
cout << sin <<" in "<<num_s<<endl;
-
}
-
charin::charin(const char* sorstr )
-
{
-
len = strlen(sorstr)+1;
-
cout << len <<" bytes mem alloc!"<<endl;
-
sin = new char[len];
-
strcpy(sin,sorstr);
-
num_s++;
-
cout << sin <<" in "<<num_s<<endl;
-
}
-
charin::~charin(void)
-
{
- num_s--;
-
cout << len <<" bytes mem release!"<<endl;
-
cout << sin <<" out "<<num_s<<endl;
- delete [] sin;
-
}
-
std::ostream & operator<<(std::ostream & os,const charin & cin)
-
{
-
os << cin.sin;
-
return os;
-
}
-
-
::::::::::::::
-
main.cpp
-
::::::::::::::
-
/*************************************************************************
-
> File Name: main.cpp
-
> Author: gaopeng
-
> Mail: gaopp_200217@163.com
-
> Created Time: Fri 24 Jun 2016 02:23:51 AM CST
-
************************************************************************/
-
-
#include<iostream>
-
#include"c2.h"
-
using namespace std;
-
-
-
void chmi(charin );
-
-
int main(void)
-
{
-
const char *testc = "test123";
-
charin t1;
-
cout <<"test"<<endl;
-
charin t2(testc);
-
charin t3 = t2; //error
-
chmi(t2); //error
-
-
}
-
-
//error beacause pointer sin pointer same point;
-
void chmi(charin a)
-
{
-
cout<<"test here: "<<a<<endl;
- }
gaopeng@bogon:~/CPLUSPLUS/part11/c2$ ./a.out
10 mem alloc!
study c++ in 1
test
8 bytes mem alloc!
test123 in 2
test here: test123
8 bytes mem release!
out 1
*** Error in `./a.out': double free or corruption (fasttop): 0x00000000021c1030 ***
Aborted (core dumped)
我使用的LINUX g++编译器,可以看报错double free or corruption,重复的释放,其实这里的计数器num_s也不能正常工作。
这种情况下我们必须要自己定义复制构造函数,使用深度复制而不是简单的为指针类型数据复制指针的指向而已。
我们在程序中加入:
点击(此处)折叠或打开
-
c2.h 加入
-
-
#include<iostream>
-
using namespace std;
-
-
class charin
-
{
-
private:
-
..........
-
public:
-
..........
-
charin(charin &); //加入
-
.........
-
};
cf.cpp加入新函数 复制构造函数
-
charin::charin(charin &in)
-
{
-
len = in.len;
-
sin = new char[len];
-
strcpy(sin,in.sin);
-
num_s++;
-
cout << len <<" bytes mem alloc!"<<endl;
- cout << sin <<" deep copy in "<<num_s<<endl;
- }
10 mem alloc!
study c++ in 1
test
8 bytes mem alloc!
test123 in 2
8 bytes mem alloc!
test123 deep copy in 3
8 bytes mem alloc!
test123 deep copy in 4
test here: test123
8 bytes mem release!
test123 out 3
8 bytes mem release!
test123 out 2
8 bytes mem release!
test123 out 1
10 bytes mem release!
study c++ out 0
没有问题了。
---endl---