实现string类过程中的一些疑问和总结
来源:互联网 发布:淘宝卖家怎么登录 编辑:程序博客网 时间:2024/06/05 14:35
1、为什么operator<< 和>>不能做为类的操作符重载呢,为什么一定还要声明为friend呢?
作为类的成员函数的时候,类对象默认为操作符的左参。
而你调用
cin >> obj
或者
cout << obj
的时候,类对象却是操作符的右参…
如果将operator<< 和>> 作为类的成员函数,那么在调用的时候就必须写成一种很奇怪
的样子: obj<<cout;
同时声明为friend可以方便操作类的成员变量。总结起来就是如下两点:
1 由于cout要在右边,所以<<和>>不能最为类的成员函数
2 如果在<<和>>中,不需要访问私有成员,就不用加firend
2、如下代码:
inline String& String::operator=(const String& other){ if (this!=&other) { delete[] m_data; if(!other.m_data) m_data=0; else { m_data = new char[strlen(other.m_data)+1]; strcpy(m_data,other.m_data); } } return *this;}
inline:内联,减少栈的开销。具体请看我的另外一篇博文。
m_data = new char[strlen(other.m_data)+1];
这里+1是因为strlen舍去了字符串结尾的 '\0' ,strcpy函数要根据 '\0' 判断结尾
string类的实现:
// mystring.h -- class definition#include <iostream>#include<string>using namespace std;class String{private: char * str; // pointer to string int len; // length of string static int num_strings; // number of objects static const int CINLIM = 80; // cin input limitpublic: // constructors and other methods String(const char * s); // constructor String(); // default constructor String(const String &); // copy constructor ~String(); // destructor int length () const { return len; } // overloaded operator methods String & operator=(const String &); String & operator=(const char *); char & operator[](int i); const char & operator[](int i) const; // overloaded operator friends friend bool operator<(const String &st, const String &st2); friend bool operator>(const String &st1, const String &st2); friend bool operator==(const String &st, const String &st2); friend ostream & operator<<(ostream & os, const String & st); friend istream & operator>>(istream & is, String & st); // static function static int HowMany();}; // mystring.cpp -- String class methods // 初始化静态类成员num_stringsint String::num_strings = 0;// static method int String::HowMany(){ return num_strings;} // class methods,要求动态分配字符串内存空间String::String(const char * s) { len = strlen(s); if(!s) str = 0; else { str = new char[len+1]; strcpy(str,s); }} String::String() { len = 4; str = new char[1]; str[0] = '\0'; num_strings++;} String::String(const String & st):len(st.len){ if(!st.str) str = 0; else { str = new char[len+1]; strcpy(str,st.str); }} String::~String() // necessary destructor{ delete str; } // overloaded operator methods // assign a String to a StringString & String::operator=(const String & st){ if (this == &st) return *this; delete [] str; len = st.len; if(!st.str) str = 0; else { str = new char[len + 1]; std::strcpy(str, st.str); } return *this;} // assign a C string to a StringString & String::operator=(const char * s){ delete [] str; len = std::strlen(s); if(!s) str = 0; else { str = new char[len + 1]; std::strcpy(str, s); } return *this;} // read-write char access for non-const Stringchar & String::operator[](int i){ if(i >=0 && i <= strlen(str)) return str[i];} // read-only char access for const Stringconst char & String::operator[](int i) const{ return static_cast<const char>(str[i]); //此处与上一空内容一样} // overloaded operator friendsbool operator<(const String &st1, const String &st2){ return (std::strcmp(st1.str, st2.str) < 0);} bool operator>(const String &st1, const String &st2){ return (std::strcmp(st1.str, st2.str) > 0);} bool operator==(const String &st1, const String &st2){ if(strlen(st1.str) != strlen(st2.str)) return false; return strcmp(st1.str,st2.str) ? false:true;} // simple String outputostream & operator<<(ostream & os, const String & st){ os << st.str <<" " << st.len << " " << st.num_strings << std::endl; return os;} // quick and dirty String inputistream & operator>>(istream & is, String & st){ is >> st.len >> st.str; return is;} int main(){ String s1 = "abd"; String s2 = s1; cout << s2[1];}
0 0
- 实现string类过程中的一些疑问和总结
- 学习过程中的一些疑问
- Css中的一些疑问
- 一些疑问和思考
- 学习过程中的一些总结
- 工作过程中的一些总结和相关的链接地址
- WebGame开发过程中的一些思考和总结
- 关于WMTS服务的一些总结和疑问
- 记录学习过程中的疑问和解决方案,不断提高自己
- 关于string类中的一些小问题的总结
- 关于ip_free中的一些疑问
- 过程中的点点滴滴【学习疑问】
- 网站运营过程中的疑问
- iOS开发过程中的疑问
- C#实现camel字符串转换(以及查阅后总结的一些其他C#中string类中的方法)
- String equals方法的一些疑问
- 一些思考,一些遐想和一些疑问
- 测试过程中的一些疑问(缺陷发现率、测试结束标准等)
- vGPU技术详解及配置要求
- 极速开发框架dhroid之sqlite优化
- 让技术人员看得懂的流程(3)——领域模型
- 让技术人员看得懂的流程(4)——设计模型
- JavaScript在HTML中的嵌入方式
- 实现string类过程中的一些疑问和总结
- 【Unity 3D】学习笔记五:Toggle(开关按钮)
- lua字符匹配
- 更多文章请移步www.vlix.org独立博客
- windows核心编程第三章学习事件内核对象代码
- ASM算法简介
- 性能测试、操作系统优化对性能测试的影响,以及如何优化操作系统
- 最新版adt_bundle的fragement_mian.xml的控件读怎么在MainActivity.java里读取出来问题(所谓的一些空指针问题)
- Ubuntu避免更新某个软件