【发布时间】:2015-10-22 08:30:57
【问题描述】:
我实现了字符串类MyString。这是代码:
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
class MyString{
private:
char * content;
int length;
void copy(const MyString & source);
public:
MyString();
MyString(const char * source);
~MyString();
MyString(const MyString & source);
void print(void);
MyString & operator = (const MyString &source);
friend std::ostream & operator << (std::ostream & out, const MyString& towrite);
friend std::istream & operator >> (std::istream & in, MyString & toread);
};
MyString::MyString(){
content = new char[1];
content[0] = '\0';
length = 0;
}
MyString::MyString(const char *source){
length = strlen(source);
content = new char[length + 1];
strcpy(content, source);
}
MyString::~MyString(){
delete[] content;
}
void MyString::copy(const MyString & source){
length = source.length;
content = new char[length + 1];
strcpy(content, source.content);
}
MyString::MyString(const MyString & source){
copy(source);
}
void MyString::print(void){
cout << "" << content << endl;
}
MyString &MyString::operator=(const MyString &source){
copy(source);
return *this;
}
std::ostream & operator<<(std::ostream & out,const MyString& towrite){
out << towrite.content;
return out;
}
std::istream & operator >> (std::istream & in, MyString & toread){
int length;
std::cout << "Enter length of word: " << endl;
std::cin >> length;
toread.length = length;
toread.content = new char[toread.length+1];
for (int i = 0; i < toread.length; i++){
in >> toread.content[i] ;
}
toread.content[toread.length] = '\0';
return in;
}
我的问题与重载运算符有关>>。
对于这个主程序:
int main(){
MyString word;
std::cout<<"Enter some word: "<<endl;
std::cin>>word;
std::cout<<"Your entered: "<<word<<endl;
}
这是输出:
Enter some word:
Enter length of word:
5
stack
Your entered: stack
Process returned 0 (0x0) execution time : 8.313 s
Press any key to continue.
它正确打印用户输入的字符串,但它不会按照我想要的方式“模仿”原始字符串类。这就是原因。
如果使用 C++ 字符串类:
int main(){
std::string word;
std::cout<<"Enter some word: "<<endl;
std::cin>>word;
std::cout<<"Your entered: "<<word<<endl;
}
用户无需输入字长。我可以通过我的班级实现这一目标吗?
编辑1:
我是这样做的:
std::istream & operator >> (std::istream & in, MyString & toread){
char *temp;
temp = new char[100];
char c;
int i = 0;
while(c != '\n'){
c = getchar();
temp[i++] = c;
}
temp[i] = '\0';
int length = i-1;
toread.length = length;
toread.content = new char[toread.length+1];
for(int i = 0 ; i < toread.length ; i++){
toread.content[i] = temp[i];
}
delete [] temp;
toread.content[toread.length+1]='\0';
}
它可以正常工作。但是,我收到警告,因为我没有返回“in”:
||=== 构建:在 fdsfsdf 中调试(编译器:GNU GCC 编译器)===| C:\Users\hae\Desktop\fdsfsdf\main.cpp||在函数'std::istream& operator>>(std::istream&, MyString&)':| C:\Users\hae\Desktop\fdsfsdf\main.cpp|137|警告:函数中没有返回语句返回非 void [-Wreturn-type]| ||=== 构建完成:0 个错误,1 个警告(0 分钟,4 秒)===| ||=== 运行:在 fdsfsdf 中调试(编译器:GNU GCC Compiler)===|
【问题讨论】:
-
是的。调用
istream::operator>>(std::string),然后从std::string构造您的字符串(使用c_str())。 -
operator>>(std::istream&, std::string&)读取直到找到第一个空白字符。你也可以那样做。此外,您正在泄漏内存。 -
你也没有释放你的记忆。首先,您的默认字符串构造函数分配内存并具有指向它的指针,然后当您获得输入时,您忘记释放该内存。我还会研究一下 std::allocator ,这将是一种更有效的分配和构造内存的方法。
-
在你的默认构造函数中你总是。赋值永远不会解除分配。
-
@elf 例如,在您的复制方法中,您应该首先删除该指针指向的对象的 char * 内存,然后使 char * 内容指向新分配的内存。其他地方也一样。
标签: c++ arrays string dynamic-allocation