class中缺省的4个函数的实现
来源:互联网 发布:小宇电玩淘宝店 编辑:程序博客网 时间:2024/06/15 03:45
class类中默认会实现4个函数,分别为默认构造函数,拷贝构造函数,析构函数和=运算符重载
#include <iostream>using namespace std;class myString{public:myString(const char *str=NULL);myString(const myString &other);~myString();myString & operator=(const myString &other);showString();private:char *m_data;};myString::~myString(){delete [] m_data;}myString::myString(const char *str){if (str == NULL){m_data = new char[1];*m_data = '\0';} else{int length = strlen(str)+1;m_data = new char[length];while(*str != '\0')*m_data++ = *str++;*m_data = '\0';}}myString::myString(const myString &other){int length = strlen(other.m_data)+1;m_data = new char(length);strcpy(m_data,other.m_data);*m_data = '\0';}myString & myString::operator=(const myString &other){if (this == &other){return *this;} delete []m_data;int length = strlen(other.m_data)+1;m_data = new char[length];strcpy(m_data,other.m_data);return *this;}myString::showString(){cout<<this->m_data<<endl;}int main(int argc,char **argv){myString str;char *p = "hello peter";myString dest(p);dest.showString();str = dest;str.showString();return 0;}
- class中缺省的4个函数的实现
- C++ 类的6个缺省函数
- 函数的缺省参数
- 函数的缺省参数
- 函数的缺省参数
- 函数的缺省参数
- DBOperation的缺省实现
- CWnd类虚函数的调用时机、缺省实现
- 构造函数的参数缺省
- php函数的缺省参数
- C++ 函数的缺省参数
- C++缺省参数的函数
- C++ Primer中关于缺省构造函数的叙述
- 继承中虚函数的缺省参数值问题
- 【C++缺省函数】 空类默认产生的6个类成员函数
- 【C++缺省函数】 空类默认产生的6个类成员函数
- python 函数的缺省参数的注意事项
- C++之缺省参数的构造函数、缺省的构造函数及缺省的析构函数
- 网站访问流量的主要统计指标
- 《转》电路设计以及PCB制作中,经常碰见电源符号:VCC、 VDD、VEE、VSS
- servlet 多线程问题
- C/C++变量在内存中的分布
- Spring
- class中缺省的4个函数的实现
- 实用滴sql语句
- CSS Sprites + 圆角
- 2011新兴产业,创业项目......等等,有哪些
- KD-tree学习
- Ruby+Watir经验谈: Understanding Watir
- 注册好久了。今天冒个泡,曾经收藏的经典
- 关于Mysql字符集的问题
- iPhone