C++学习之路 day4
来源:互联网 发布:linux常用命令举例 编辑:程序博客网 时间:2024/06/06 01:26
Day4知识点:
myString类对象自我实现以及部分实验源代码:
//myString.h#ifndef MYSTRING_H#define MYSTRING_H#include <stdio.h>class myString{public: myString(const char *p =NULL); myString(const myString& another); myString& operator=(const myString& another); //运算符重载 myString operator+(const myString& another); bool operator>(const myString& another); bool operator<(const myString& another); bool operator==(const myString& another); char& operator[](int idx); char at(int idx); char* str();private: char* _str;};#endif // MYSTRING_H
//myString.cpp#include "mystring.h"#include <string.h>myString::myString(const char* p){ if(p == NULL) { _str =new char[1]; *_str = '\0'; } else { _str = new char[strlen(p)+1]; strcpy(_str,p); }}myString::myString(const myString& another){ _str = new char[strlen(another._str)+1]; strcpy(_str,another._str);}myString& myString::operator=(const myString& another){ if(this == &another) return *this; delete []_str; _str = new char[strlen(another._str)+1]; strcpy(_str,another._str); return *this;}myString myString::operator+(const myString& another){ myString tmp; delete []tmp._str; int len = strlen(this->_str); len +=strlen(another._str); tmp._str = new char[len+1]; memset(tmp._str,0,len+1); strcat(tmp._str,this->_str); strcat(tmp._str,another._str); return tmp;}bool myString::operator>(const myString& another){ if(strcmp(this->_str,another._str)>0) return true; else return false;}bool myString::operator<(const myString& another){ if(strcmp(this->_str,another._str)<0) return true; else return false;}bool myString::operator==(const myString& another){ if(strcmp(this->_str,another._str)==0) return true; else return false;}char& myString::operator[](int idx){ return this->_str[idx];}char myString::at(int idx){ return this->_str[idx];}char* myString::str(){ return _str;}
//main函数#include <iostream>#include "mystring.h"using namespace std;#if 0int main(){// string a("***************");// string b;// string c("china");// cout<<a<<endl;// cout<<b;// cout<<c<<endl; myString a("***************"); myString b; myString c("china"); cout<<a.str()<<endl; cout<<b.str(); cout<<c.str()<<endl; myString s("aa"); myString s3(s); myString s4; s4 = s; myString s5("bb"); s5 = s5; cout<<s.str()<<endl; cout<<s3.str()<<endl; cout<<s4.str()<<endl; cout<<s5.str()<<endl; return 0;}#endif#if 0int main(){ myString s1("abc"); myString s2("def"); myString s3 = s1 + s2; cout<<s3.str()<<endl;}#endifint main(){ myString s1("bbc"); myString s2("aef"); if(s1>s2) cout<<"s1 > s2"<<endl; else if(s1 ==s2) cout<<"s1 == s2"<<endl; else cout<<"s1 < s2"<<endl; return 0;}
1. 拷贝构造器,格式:A(const A & another) ;若不提供,系统会自动提供默认的。
myString::mystring(const myString& another){ _str = another._str; //此处_str为myString类的私有成员,在同类之间是没有隐私的}
2.如2中的浅拷贝myString s(s3);(在类中开辟空间的情况下)若不作处理或用系统提供的默认构造函数,会导致内存的重析构 double free,原因(s和s3的_str指向相同的内存空间,在析构时会析构两次)。应该如此修改
myString::myString(const myString& another){ _str = new char[strlen(another._str)+1]; strcpy(_str,another._str);}
3.拷贝构造
1) 格式 类名(const 类名 &another),依附于原有对象创建新对象;
A a;A b(a);A c = b;
2)若不提供,系统会提供默认,一旦自定义,系统不再提供默认;
3)系统提供的默认,是一种等位拷贝。也就是浅拷贝;
4)浅拷贝,在某些情况下,会导致内存重析构(如2所示,含有堆空间的情况下),即double free(有些系统编译时提示错误,有些系统编译通过).要自己实现拷贝构造。
4.赋值重载
1)格式 类名 operator=(const myString& another);两个对象都已经创建好之后的赋值行为。
A c;A a;c = a;
2)编译器提供默认,一旦自定义,系统不再提供默认;
3)默认赋值运算符也是一种等位赋值,浅赋值;
4)浅赋值,可能会导致:a)自身发生内存泄漏;b)内存发生重析构;c)自赋值。应该如此修改
myString& myString::operator=(const myString& another){ if(this == &another) return *this; delete []_str; _str = new char[strlen(another._str)+1]; strcpy(_str,another._str); return *this;}
5.建立类时的必要构造函数
Class A{pubilc: A(); //一般情况下要标配,即无参的情况包含默认数据,如A(int i = 0):a(i){} ~A(); A(const A& another); A& operator=(const A& another);}
阅读全文
1 0
- python学习之路Day4
- C++学习之路 day4
- Mysql学习之路——day4
- iOS学习之旅之C语言day4
- c语言学习日志 day4
- python自动化之路-day4
- Java学习(回顾)笔记之day4
- openwrt之wrtnode学习日记day4
- Android学习之Java基础篇day4
- 爬虫之路——Day4
- python自动化之路-day4.1
- jQuery源码学习 之 ready与load事件 ---- day4
- C语言:Day1~Day4
- java学习笔记day4
- CCNA学习笔记Day4
- HTML5学习-Day4
- 学习笔记Day4
- Android学习笔记day4
- 297
- 298
- 299
- weex
- block解决循环引用(二)
- C++学习之路 day4
- 安卓SATA自动挂载实例
- Myeclipse2014下的maven的安装与使用
- Kotlin官方文档翻译,类和对象:类和继承
- 三星7nm生产线提前动工 与台积电抢订单
- 博客迁移至博客园
- JS的解析机制(作用域、变量提升等)和两个例子
- MySql中的FIND_IN_SET使用方法,满足包含查询,类似like但是不同于like的用法
- android关于AndroidManifest.xml详细分析