string类构造函数、拷贝构造函数、赋值函数、析构函数
2014-02-05 20:52
274 查看
#ifndef DDSTRING_H #define DDSTRING_H class ddString { public: ddString(const char *pStrAddr = 0); ddString(const ddString &otherStr); ddString &operator=(const ddString &otherStr); unsigned int size() const; char *getData() const; ~ddString(); private: char *pddStr; }; #endif
#include <string.h> #include "ddString.h" ddString::ddString(const char *pStrAddr) { if (pStrAddr) { unsigned int size = strlen(pStrAddr); pddStr = new char[size+1]; strcpy(pddStr, pStrAddr); } else { pddStr = new char[1]; *pddStr = '\0'; } } ddString::ddString(const ddString &otherStr) { unsigned int size = otherStr.size(); pddStr = new char[size+1]; strcpy(pddStr, otherStr.pddStr); } ddString &ddString::operator=(const ddString &otherStr) { //if they have same pointers, they are same if (this != &otherStr) { //delete old resource delete[] pddStr; unsigned int size = otherStr.size(); pddStr = new char[size+1]; strcpy(pddStr, otherStr.pddStr); } return(*this); } unsigned int ddString::size() const { return(strlen(pddStr)); } char *ddString::getData() const { return(pddStr); } ddString::~ddString() { if (pddStr != 0) { delete[] pddStr; pddStr = 0; } }
相关文章推荐
- String类的构造函数、拷贝构造函数、赋值函数和析构函数
- string类的构造函数,析构函数,拷贝构造函数还有就是赋值函数,然后加上strcpy的函数
- string类的构造函数,拷贝构造函数,析构函数和赋值函数
- 编写String类的构造函数、析构函数、拷贝构造函数、赋值函数
- 编写string类的构造函数、拷贝构造函数、析构函数、赋值函数
- 写String类的普通构造函数,析构函数,拷贝构造函数和赋值函数
- 自主编程实现String类的构造函数,析构函数,拷贝构造函数,赋值函数
- string类的构造函数、析构函数、拷贝构造函数和赋值函数
- 编写String类的普通构造函数、拷贝构造函数、析构函数、赋值函数
- string类的构造函数,拷贝构造函数,析构函数和赋值函数
- 编写String类的构造函数、析构函数以及赋值函数(C++面试题)
- C/C++学习笔记:String类的构造函数、析构函数和赋值函数
- 编写类String的构造函数、拷贝构造函数、析构函数、赋值函数
- String类的简易实现,用到了构造函数、析构函数、重载运算符、拷贝构造函数、友元函数等知识
- c++笔记之赋值函数,拷贝构造函数,构造函数,析构函数
- 编写一个String类,实现其构造函数、析构函数以及赋值函数
- 算法25:实现String类的构造函数,拷贝构造函数和赋值函数
- 构造函数,拷贝构造函数,赋值函数,析构函数
- 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数
- String 构造函数,析构函数,拷贝构造函数和赋值函数