构造函数、析构函数、拷贝构造函数、赋值函数
2011-02-10 20:43
239 查看
一个小例子,说明类的几大函数:
#include <iostream>using namespace std;class c_test{ public: c_test(); c_test(int para_a=3,string str="original"); c_test(const c_test & other); ~c_test(void); c_test & operator =(const c_test & other); private: string name; int num;};c_test::c_test(){}c_test::c_test(int para_a,string str){ this->num=para_a; this->name=str; cout<<"I am in constructor, my name is "<<this->name<<", my value is "<<this->num<<endl;}c_test::c_test(const c_test & other){ this->num=other.num; this->name=other.name+" copy"; cout<<"I am in copy constructor, my name is "<<this->name<<", my value is "<<this->num<<endl;}c_test & c_test::operator=(const c_test & other){ this->num=other.num; this->name=other.name; cout<<"I am in operator =, my name is "<<this->name<<", my value is "<<this->num<<endl; return *this;}c_test::~c_test(void){ cout<<"I am in destructor, my name is "<<this->name<<", my value is "<<this->num<<endl;}main(){ int a; int b; c_test obj_a(3,"a"); c_test obj_b(obj_a);//调用拷贝构造函数 c_test * p_a=new c_test(333,"s"); c_test * p_b=new c_test(2,"b"); *p_a=obj_a;//调用赋值函数 printf("0X%08X\n",&a); printf("0X%08X\n",&b); printf("0X%08X\n",p_a); delete p_b;}
#include <iostream>using namespace std;class c_test{ public: c_test(); c_test(int para_a=3,string str="original"); c_test(const c_test & other); ~c_test(void); c_test & operator =(const c_test & other); private: string name; int num;};c_test::c_test(){}c_test::c_test(int para_a,string str){ this->num=para_a; this->name=str; cout<<"I am in constructor, my name is "<<this->name<<", my value is "<<this->num<<endl;}c_test::c_test(const c_test & other){ this->num=other.num; this->name=other.name+" copy"; cout<<"I am in copy constructor, my name is "<<this->name<<", my value is "<<this->num<<endl;}c_test & c_test::operator=(const c_test & other){ this->num=other.num; this->name=other.name; cout<<"I am in operator =, my name is "<<this->name<<", my value is "<<this->num<<endl; return *this;}c_test::~c_test(void){ cout<<"I am in destructor, my name is "<<this->name<<", my value is "<<this->num<<endl;}main(){ int a; int b; c_test obj_a(3,"a"); c_test obj_b(obj_a);//调用拷贝构造函数 c_test * p_a=new c_test(333,"s"); c_test * p_b=new c_test(2,"b"); *p_a=obj_a;//调用赋值函数 printf("0X%08X\n",&a); printf("0X%08X\n",&b); printf("0X%08X\n",p_a); delete p_b;}
相关文章推荐
- 构造函数、拷贝构造函数、赋值函数、析构函数的调用顺序
- 实现MyString类--构造函数、拷贝构造函数、析构函数、赋值函数、操作符重载函数
- 类String的 构造函数、拷贝构造函数、析构函数、赋值函数实现
- String 的普通构造函数、拷贝构造函数、析构函数、赋值函数
- c++笔记之赋值函数,拷贝构造函数,构造函数,析构函数
- String类的构造函数、拷贝构造函数、赋值函数和析构函数
- 类的构造函数、析构函数、拷贝构造函数与赋值函数
- 编写String类的构造函数、析构函数、拷贝构造函数、赋值函数
- 读书笔记:构造函数、析构函数、拷贝构造函数、赋值函数
- C++类构造函数,拷贝构造函数,赋值函数,析构函数几点注意
- 构造函数,拷贝构造函数,赋值函数,析构函数
- 类String的构造函数、析构函数、拷贝构造函数和赋值函数
- 面试题目6 编写类String的构造函数、拷贝构造函数、析构函数、赋值函数
- C++实现一个类并包含构造函数,拷贝构造函数,赋值函数,析构函数
- 编写String的构造函数、析构函数、拷贝构造函数、赋值函数
- string类的构造函数,析构函数,拷贝构造函数还有就是赋值函数,然后加上strcpy的函数
- 构造函数,析构函数,拷贝构造函数和赋值函数
- 类String的构造函数、拷贝构造函数、析构函数、赋值函数
- string类的构造函数,拷贝构造函数,析构函数和赋值函数
- 实现C++类中默认的四个方法默认构造函数、析构函数、拷贝构造函数、赋值函数