创建不带字符串库的字符串类
Create string class without strings library
我正在尝试在不使用字符串库(即#include <string>
)的情况下创建自己的字符串类
我可以使用c样式字符串!
我必须使iString
ctor、copy ctor、dtor和重载运算符>>,<lt;,+,*
到目前为止,我的问题是,当我试图编译我的作品时,我的operator+
和operator*
都会出现这个错误。。。即:
warning: reference to local variable s3 returned [enabled by default]
s3
只是iString
对象名称。
这是我目前所拥有的:
.h文件
#ifndef __ISTRING_H__
#define __ISTRING_H__
struct iString{
char * chars;
unsigned int length;
unsigned int capacity;
iString(); // ctor
iString(const char *); // copy ctor
iString(const iString&); // copy ctor
~iString(); // dtor
};
std::istream &operator>>(std::istream &in, const iString &s);
std::ostream &operator<<(std::ostream &out, const iString &s);
iString &operator+(const iString &s1, const iString &s2);
iString &operator+(const iString &s1, const char *c_str);
iString &operator*(const iString &s, const int k);
iString &operator*(const int k, const iString &s);
#endif
.cc文件
#include <iostream>
#include "istring.h"
#include <cstring>
using namespace std;
iString::iString(){ // this is my ctor
chars = new char[1];
length = 0;
capacity = length + 1;
chars[0] = ' ';
}
iString::iString(const char *word){ // this is my copy ctor from a c-string
length = strlen(word);
capacity = length + 1;
chars = new char[capacity];
strcpy(chars, word);
}
iString::iString(const iString &s){ // this is my copy ctor from an iString
length = strlen(s.length);
capacity = length + 1;
chars = new char[capacity];
strcpy(chars, s.chars);
}
iString::~iString() { //dtor
delete [] this->chars;
}
istream &operator>>(istream &in, const iString &s){ // not done, don't worry
}
ostream &operator<<(ostream &out, const iString &s){
//out<<s.chars;
return out<<s.chars;
}
iString &operator+(const iString &s1, const iString &s2){
iString s3;
char *new_str;// = new char[size];
new_str = strcat(s1.chars, s2.chars);
s3 = iString(new_str);
return s3;
}
// tried a different approach
iString &operator+(const iString &s1, const char *c_str){
int size = s1.length + strlen(c_str) + 1;
char *new_str = new char[size];
strcpy(new_str, s1.chars);
strcat(new_str, c_str);
iString s3(new_str);
delete [] new_str;
return s3;
}
iString &operator*(const iString &s, const int k){
int size = (s.length * k) + 1;
char *c_str = new char[size];
for(int i = 0; i < k; i++){
strcat(c_str, s.chars);
}
iString t(c_str);
delete[] c_str;
return t;
}
iString &operator*(const int k, const iString &s){
return s * k;
}
您不应该从operator+( , )
返回引用
iString operator+(const iString &s1, const iString &s2){
...
}
问题是s3在返回后会被破坏,所以当调用方最终获得字符串时,它将是对已经被破坏的字符串的引用,因此是无效的。
I.E:您返回的是对函数本地字符串的引用——当您到达调用方时,它将不再存在。
你需要的是按价值回报。这样,字符串在返回时仍然有效。众所周知,对于C++11,只要提供移动构造和赋值,就可以使用右值引用,这是非常有效的。此外,在C++98中,您可以指望许多编译器在许多情况下应用返回值优化来提高效率。
警告表示您正在返回对局部变量对象的引用。这是一个问题,因为当函数返回时,局部变量会被破坏。
std::string
的operator+
返回一个新的字符串对象,而不是对字符串对象的引用;你可能也应该这么做。替代方案是创建一个新对象并返回一个指针,或者修改传递给运算符的一个字符串,这两者都不太好。
相关文章:
- 从std::字符串创建OssBitString
- 如何从字符串创建lua表?
- 如何使用循环从另一个字符串创建一个没有空格的新字符串
- 如何从字符串创建一个web::uri以放入client.connect()
- 如何从字符串创建固定的QDateTime
- 用C 的字符串创建一系列字符串
- 正在尝试为c字符串创建一个Text Line类
- 如何使用字符串创建条件
- 优化字符串创建
- C++:使用子字符串创建新字符串
- 从字符串创建对象 (C++)
- 通过本机字符串创建 BSTR(使用_bstr_t包装器)时,如何设置长度
- 基于另一个字符串创建一个由空格 (_) 和空格组成的字符串
- 有没有更好的方法从字符串创建目录?CPP,创建目录,字符串流,字符串
- 在 c++ 中从字符串创建类以避免大型 switch 语句
- 从字符串C++创建控制台标题
- C++:从字符串创建参数化指针
- 在C++中为字符串创建自定义函数
- 如何为字符串创建要起作用的unordered_map
- 如何从二进制字符串创建一个bitset