#include<iostream>
using namespace std;
class mystring:public string {
private:
string store;
public:
friend class string;
mystring(char *s):store(s) {};
// mystring(string s):store(s) {};
mystring():store() {};
mystring(mystring &t):store(t.store) {};
mystring &operator=(mystring &t) {
store = t.store;
return *this;
};
mystring &operator=(char *s) {
store = s;
return *this;
};
friend mystring operator+(mystring &a, mystring &b);
friend mystring operator+(mystring &a, char *s);
friend mystring operator+(char *s, mystring &a);
mystring &operator+=(char *s) {
store += s;
return *this;
};
friend ostream &operator<<(ostream &output, mystring &t);
char &operator[](int ptr) {
return store[ptr];
};
};
mystring operator+(mystring &a, mystring &b) {
mystring tmp;
tmp.store = a.store + b.store;
return tmp;
};
mystring operator+(mystring &a, char *s) {
mystring tmp = a;
tmp += s;
return tmp;
};
mystring operator+(char *s, mystring &a) {
mystring tmp(s);
tmp = tmp + a;
return tmp;
};
ostream &operator<<(ostream &output, mystring &t) {
output << t.store;
return output;
};
int comparestring(const void * e1, const void *e2) {
mystring *s1 = (mystring *) e1;
mystring *s2 = (mystring *) e2;
if (*s1 < *s2)
return -1;
else if (*s1 == *s2)
return 0;
else if (*s1 > *s2)
return 1;
};
int main() {
mystring s1("abcd-"), s2, s3("efgh-"), s4(s1);
mystring sarray[4] = {"big", "me", "about", "take"};
cout << "1. " << s1 << s2 << s3 << s4 << endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = mystring("qrst-") + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
// qsort(sarray, 4, sizeof(mystring), comparestring);
for (int i = 0; i < 4; i ++)
cout << sarray[i] << endl;
// cout << s1(0, 4) << endl;
// cout << s1(5, 10) << endl;
return 0;
};
出了什么问题?
10 个解决方案
#1
顶一下
#2
记得C++ primer有一句话……“绝对不要派生string”
STL是给你用的,不是给你扩充折腾的,他是一个整体环环相扣,绝对不能随便扩充修改
STL是给你用的,不是给你扩充折腾的,他是一个整体环环相扣,绝对不能随便扩充修改
#3
class mystring:public string {
private:
string store;
public:
friend class string;
光是上面这几句就已经很雷人了……
#4
我们正在学类和对象,这是老师布置的作业啊,让我们用string派生出mystring来扩充出str(4, 9)这样的功能,现在只是复制string的功能我都没做好,这该怎么办啊?
顺便问下怎么像4l那样发code?
顺便问下怎么像4l那样发code?
#5
问题出在临时变量上,而非const引用是不能绑定临时变量(右值)
比如
mystring operator+(char *s, mystring &a) {
mystring tmp(s);
tmp = tmp + a;//tmp + a后会产出一个mystring类型的临时变量
//而你定义的mystring &operator=(mystring &t)不能接受临时变量作为t的实参
//要改成mystring &operator=( const mystring &t)
return tmp;
};
同样,mystring(mystring &t)、mystring operator+(mystring &a, mystring &b)等
要改成
mystring( const mystring &t)、mystring operator+( const mystring &a, const mystring &b)
比如
mystring operator+(char *s, mystring &a) {
mystring tmp(s);
tmp = tmp + a;//tmp + a后会产出一个mystring类型的临时变量
//而你定义的mystring &operator=(mystring &t)不能接受临时变量作为t的实参
//要改成mystring &operator=( const mystring &t)
return tmp;
};
同样,mystring(mystring &t)、mystring operator+(mystring &a, mystring &b)等
要改成
mystring( const mystring &t)、mystring operator+( const mystring &a, const mystring &b)
#include<string>
#include<iostream>
using namespace std;
class mystring:public string
{
private:
string store;
public:
friend class string;
mystring(const char *s):store(s) {}
// mystring(string s):store(s) {}
mystring():store() {}
mystring(const mystring &t):store(t.store) {}
mystring &operator=(const mystring &t)
{
store = t.store;
return *this;
}
mystring &operator=(const char *s)
{
store = s;
return *this;
}
friend mystring operator+(const mystring &a, const mystring &b);
friend mystring operator+(const mystring &a, char *s);
friend mystring operator+(const char *s, const mystring &a);
mystring &operator+=(const char *s)
{
store += s;
return *this;
}
friend ostream &operator<<(ostream &output, const mystring &t);
char &operator[](int ptr)
{
return store[ptr];
}
};
mystring operator+(const mystring &a, const mystring &b)
{
mystring tmp;
tmp.store = a.store + b.store;
return tmp;
}
mystring operator+(const mystring &a, const char *s)
{
mystring tmp = a;
tmp += s;
return tmp;
}
mystring operator+(const char *s, const mystring &a)
{
mystring tmp(s);
tmp = tmp + a;
return tmp;
}
ostream &operator<<(ostream &output, const mystring &t)
{
output << t.store;
return output;
}
int comparestring(const void * e1, const void *e2)
{
mystring *s1 = (mystring *) e1;
mystring *s2 = (mystring *) e2;
if (*s1 < *s2)
return -1;
else if (*s1 == *s2)
return 0;
else if (*s1 > *s2)
return 1;
}
int main()
{
mystring s1("abcd-"), s2, s3("efgh-"), s4(s1);
mystring sarray[4] = {"big", "me", "about", "take"};
cout << "1. " << s1 << s2 << s3 << s4 << endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = mystring("qrst-") + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
// qsort(sarray, 4, sizeof(mystring), comparestring);
for (int i = 0; i < 4; i ++)
cout << sarray[i] << endl;
// cout << s1(0, 4) << endl;
// cout << s1(5, 10) << endl;
return 0;
}
#6
赞6l!多谢多谢!
还是顺便问下怎么这样发code……
还是顺便问下怎么这样发code……
#7
点插入源代码符号([字体颜色]右边那个),然后把代码放在[code=..][code]之间就OK了.
#8
在回复的工具栏上有一个#型的图标(在字体颜色后面),点击选择C\C++ 会出现 在中间添加代码即可,比如:
#include<string>
#include<iostream>
using namespace std;
#9
#include<iostream>
using namespace std;
int main()
{
cout << "hello world!" << endl;
return 0;
};
#10
赞!多谢诸位,那就结贴了!
#1
顶一下
#2
记得C++ primer有一句话……“绝对不要派生string”
STL是给你用的,不是给你扩充折腾的,他是一个整体环环相扣,绝对不能随便扩充修改
STL是给你用的,不是给你扩充折腾的,他是一个整体环环相扣,绝对不能随便扩充修改
#3
class mystring:public string {
private:
string store;
public:
friend class string;
光是上面这几句就已经很雷人了……
#4
我们正在学类和对象,这是老师布置的作业啊,让我们用string派生出mystring来扩充出str(4, 9)这样的功能,现在只是复制string的功能我都没做好,这该怎么办啊?
顺便问下怎么像4l那样发code?
顺便问下怎么像4l那样发code?
#5
问题出在临时变量上,而非const引用是不能绑定临时变量(右值)
比如
mystring operator+(char *s, mystring &a) {
mystring tmp(s);
tmp = tmp + a;//tmp + a后会产出一个mystring类型的临时变量
//而你定义的mystring &operator=(mystring &t)不能接受临时变量作为t的实参
//要改成mystring &operator=( const mystring &t)
return tmp;
};
同样,mystring(mystring &t)、mystring operator+(mystring &a, mystring &b)等
要改成
mystring( const mystring &t)、mystring operator+( const mystring &a, const mystring &b)
比如
mystring operator+(char *s, mystring &a) {
mystring tmp(s);
tmp = tmp + a;//tmp + a后会产出一个mystring类型的临时变量
//而你定义的mystring &operator=(mystring &t)不能接受临时变量作为t的实参
//要改成mystring &operator=( const mystring &t)
return tmp;
};
同样,mystring(mystring &t)、mystring operator+(mystring &a, mystring &b)等
要改成
mystring( const mystring &t)、mystring operator+( const mystring &a, const mystring &b)
#include<string>
#include<iostream>
using namespace std;
class mystring:public string
{
private:
string store;
public:
friend class string;
mystring(const char *s):store(s) {}
// mystring(string s):store(s) {}
mystring():store() {}
mystring(const mystring &t):store(t.store) {}
mystring &operator=(const mystring &t)
{
store = t.store;
return *this;
}
mystring &operator=(const char *s)
{
store = s;
return *this;
}
friend mystring operator+(const mystring &a, const mystring &b);
friend mystring operator+(const mystring &a, char *s);
friend mystring operator+(const char *s, const mystring &a);
mystring &operator+=(const char *s)
{
store += s;
return *this;
}
friend ostream &operator<<(ostream &output, const mystring &t);
char &operator[](int ptr)
{
return store[ptr];
}
};
mystring operator+(const mystring &a, const mystring &b)
{
mystring tmp;
tmp.store = a.store + b.store;
return tmp;
}
mystring operator+(const mystring &a, const char *s)
{
mystring tmp = a;
tmp += s;
return tmp;
}
mystring operator+(const char *s, const mystring &a)
{
mystring tmp(s);
tmp = tmp + a;
return tmp;
}
ostream &operator<<(ostream &output, const mystring &t)
{
output << t.store;
return output;
}
int comparestring(const void * e1, const void *e2)
{
mystring *s1 = (mystring *) e1;
mystring *s2 = (mystring *) e2;
if (*s1 < *s2)
return -1;
else if (*s1 == *s2)
return 0;
else if (*s1 > *s2)
return 1;
}
int main()
{
mystring s1("abcd-"), s2, s3("efgh-"), s4(s1);
mystring sarray[4] = {"big", "me", "about", "take"};
cout << "1. " << s1 << s2 << s3 << s4 << endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = mystring("qrst-") + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
// qsort(sarray, 4, sizeof(mystring), comparestring);
for (int i = 0; i < 4; i ++)
cout << sarray[i] << endl;
// cout << s1(0, 4) << endl;
// cout << s1(5, 10) << endl;
return 0;
}
#6
赞6l!多谢多谢!
还是顺便问下怎么这样发code……
还是顺便问下怎么这样发code……
#7
点插入源代码符号([字体颜色]右边那个),然后把代码放在[code=..][code]之间就OK了.
#8
在回复的工具栏上有一个#型的图标(在字体颜色后面),点击选择C\C++ 会出现 在中间添加代码即可,比如:
#include<string>
#include<iostream>
using namespace std;
#9
#include<iostream>
using namespace std;
int main()
{
cout << "hello world!" << endl;
return 0;
};
#10
赞!多谢诸位,那就结贴了!