//
// main.cpp
// C++Test31
//
#include <iostream>
using namespace std;
//C++拷贝构造函数详解
class CExample
{
private:
int a;
public:
//构造函数
CExample(int b)
{
a=b;
printf("constructor is called\n");
}
//拷贝构造函数
CExample(const CExample & c)
{
a=c.a;
printf("copy constructor is called\n");
}
//析构函数
~CExample()
{
cout<<"destructor is called\n";
}
void Show()
{
cout<<a<<endl;
}
};
void g_fun(CExample c)
{
cout<<"g_func"<<endl;
}
CExample g_fun1() {
CExample temp(0);
cout<<"g_func1"<<endl;
return temp;
}
int main(int argc, const char * argv[]) {
CExample A(100);
CExample B=A;
B.Show();
// g_fun(A);
g_fun1();
return 0;
}
//constructor is called
//copy constructor is called
//100
//destructor is called
//destructor is called
//constructor is called
//copy constructor is called
//100
//copy constructor is called
//g_func
//destructor is called
//destructor is called
//destructor is called
//constructor is called
//copy constructor is called
//100
//constructor is called
//g_func1
//destructor is called
//destructor is called
//destructor is called
//
// main.cpp
// C++Test32
//
#include <iostream>
using namespace std;
class Rect
{
public:
Rect()
{
count++;
}
~Rect()
{
count--;
}
static int getCount()
{
return count;
}
private:
int width;
int height;
static int count;
};
int Rect::count=0;
int main(int argc, const char * argv[]) {
Rect rect1;
cout<<"The count of Rect:"<<Rect::getCount()<<endl;
Rect rect2(rect1);
cout<<"The count of Rect:"<<Rect::getCount()<<endl;
return 0;
}
//The count of Rect:1
//The count of Rect:1
//
// main.cpp
// C++Test33
//
#include <iostream>
using namespace std;
class Rect
{
public:
Rect()
{
count++;
}
Rect(const Rect& r)
{
width=r.width;
height=r.height;
count++;
}
~Rect()
{
count--;
}
static int getCount()
{
return count;
}
private:
int width;
int height;
static int count;
};
int Rect::count=0;
int main(int argc, const char * argv[]) {
Rect rect1;
cout<<"The count of Rect:"<<Rect::getCount()<<endl;
Rect rect2(rect1);
cout<<"The count of Rect:"<<Rect::getCount()<<endl;
return 0;
}
//The count of Rect:1
//The count of Rect:2
//
// main.cpp
// C++Test34
//
#include <iostream>
// 浅拷贝
using namespace std;
class Rect
{
public:
Rect()
{
p=new int(100);
}
Rect(const Rect& r)
{
width=r.width;
height=r.height;
p = new int(100);
*p = *(r.p);
}
~Rect()
{
assert(p!=NULL);
delete p;
}
public:
int width;
int height;
int *p;
};
int main(int argc, const char * argv[]) {
Rect rect1;
rect1.width = 100;
cout << rect1.width << endl;
Rect rect2(rect1);
cout << rect2.width << endl;
return 0;
}
//
// main.cpp
// C++Test35
//
#include <iostream>
using namespace std;
//防止按值传递
class CExample
{
public:
//构造函数
CExample(int b)
{
a = b;
cout<<"creat: "<<a<<endl;
}
private:
//拷贝构造函数,只是声明,可以申明多个,由编译器根据上下文决定选择哪一个
CExample(const CExample& C);
CExample(CExample& C);
public:
~CExample()
{
cout<< "delete: "<<a<<endl;
}
void Show ()
{
cout<<a<<endl;
}
private:
int a;
};
void g_Fun(CExample C)
{
cout<<"c"<<endl;
}
int main(int argc, const char * argv[]) {
CExample c(1);
// g_Fun(c); //按值传递将出错 Calling a private constructor of class 'CExample'
return 0;
}
参考https://www.cnblogs.com/alantu2018/p/8459250.html