将字符串重新定义为 class
Redefining String as class
我必须将 String 重新定义为 class,并且对 operator+ 重载或复制构造函数有疑问。我的 main() 编译但在 return 中没有给出任何内容或涂鸦。这是 class 字符串的片段:
class String {
char *nap;
public:
String(const char* ns){
nap=strcpy(new char[strlen(ns)+1],ns);
}
String(const String & n){
nap=strcpy(new char[strlen(n.nap)+1],n.nap);
}
String operator+(const String& n) const;
String operator+(const char* ns) const;
//operator=
String& operator=(const String &n){
if(this==&n)
return *this;
delete []nap;
nap= strcpy(new char[strlen(n.nap)+1],n.nap);
return *this;
}
//...
friend String operator+(const char*, const String&);
friend ostream& operator<<(ostream&, const String&);
};
String String::operator+(const String& s) const{
return String(nap+*s.nap);
}
String String:: operator+(const char* c) const{
return String(nap+*c);
}
String operator+(const char* c,const String & s){
return String(String(s)+String(c));
}
ostream &operator<<(ostream& os,const String& s){
os<<s.nap<<endl;
return os;
}
这里是主要的:
String s ="To "+String("be ") + "or not to be";
cout<<s<<endl;
在您的 operator+ 中调用 strcat (or better strncat) 而不是添加指针。
或者通过将一个小睡的字节复制到另一个小睡的结尾来自己完成。
在这两种情况下,您都必须确保分配了足够的内存!
我觉得加法运算符不正确。
*
运算符可以读作的内容。所以 *s.nap
实际上是 s.nap
的内容,它是 char
代表 nap
指向的第一个字符。所以 nap+*s.nap
不是你想要的,nap+*c
.
也不是
您还需要为您的 class 提供析构函数,以确保 nap
指向的内存被删除。
class String {
char *nap;
public:
// Default argument is nifty !!
String(const char* ns=""){
nap=strcpy(new char[strlen(ns)+1],ns);
}
// !! Don'te forget to delete[] on destruction
~String() {
delete[] nap;
}
String(const String & n){
nap=strcpy(new char[strlen(n.nap)+1],n.nap);
}
String operator+(const String& n) const;
// Not necessary since String(const char *) exists
// an expression like String+"X" will be casted to String+String("X")
// String operator+(const char* ns) const;
//operator=
String& operator=(const String &n){
if(this==&n)
return *this;
delete []nap;
nap= strcpy(new char[strlen(n.nap)+1],n.nap);
return *this;
}
//...
friend String operator+(const char*, const String&);
friend std::ostream& operator<<(std::ostream&, const String&);
};
// Make enough space for both strings
// concatenate
// !! delete the buffer
String String::operator+(const String& si) const {
char *n = new char [strlen(nap)+strlen(si.nap)+1];
strcpy(n,nap);
strcpy(n+strlen(nap),si.nap);
String so = String(n);
delete [] n;
return so;
}
// Not necessary. Since String(const char *) exists
// String String:: operator+(const char* c) const{
// return String(nap+*c);
// }
String operator+(const char* c,const String & s){
return String(String(s)+String(c));
}
std::ostream &operator<<(std::ostream& os,const String& s){
os<<s.nap<<std::endl;
return os;
}
我必须将 String 重新定义为 class,并且对 operator+ 重载或复制构造函数有疑问。我的 main() 编译但在 return 中没有给出任何内容或涂鸦。这是 class 字符串的片段:
class String {
char *nap;
public:
String(const char* ns){
nap=strcpy(new char[strlen(ns)+1],ns);
}
String(const String & n){
nap=strcpy(new char[strlen(n.nap)+1],n.nap);
}
String operator+(const String& n) const;
String operator+(const char* ns) const;
//operator=
String& operator=(const String &n){
if(this==&n)
return *this;
delete []nap;
nap= strcpy(new char[strlen(n.nap)+1],n.nap);
return *this;
}
//...
friend String operator+(const char*, const String&);
friend ostream& operator<<(ostream&, const String&);
};
String String::operator+(const String& s) const{
return String(nap+*s.nap);
}
String String:: operator+(const char* c) const{
return String(nap+*c);
}
String operator+(const char* c,const String & s){
return String(String(s)+String(c));
}
ostream &operator<<(ostream& os,const String& s){
os<<s.nap<<endl;
return os;
}
这里是主要的:
String s ="To "+String("be ") + "or not to be";
cout<<s<<endl;
在您的 operator+ 中调用 strcat (or better strncat) 而不是添加指针。 或者通过将一个小睡的字节复制到另一个小睡的结尾来自己完成。 在这两种情况下,您都必须确保分配了足够的内存!
我觉得加法运算符不正确。
*
运算符可以读作的内容。所以 *s.nap
实际上是 s.nap
的内容,它是 char
代表 nap
指向的第一个字符。所以 nap+*s.nap
不是你想要的,nap+*c
.
您还需要为您的 class 提供析构函数,以确保 nap
指向的内存被删除。
class String {
char *nap;
public:
// Default argument is nifty !!
String(const char* ns=""){
nap=strcpy(new char[strlen(ns)+1],ns);
}
// !! Don'te forget to delete[] on destruction
~String() {
delete[] nap;
}
String(const String & n){
nap=strcpy(new char[strlen(n.nap)+1],n.nap);
}
String operator+(const String& n) const;
// Not necessary since String(const char *) exists
// an expression like String+"X" will be casted to String+String("X")
// String operator+(const char* ns) const;
//operator=
String& operator=(const String &n){
if(this==&n)
return *this;
delete []nap;
nap= strcpy(new char[strlen(n.nap)+1],n.nap);
return *this;
}
//...
friend String operator+(const char*, const String&);
friend std::ostream& operator<<(std::ostream&, const String&);
};
// Make enough space for both strings
// concatenate
// !! delete the buffer
String String::operator+(const String& si) const {
char *n = new char [strlen(nap)+strlen(si.nap)+1];
strcpy(n,nap);
strcpy(n+strlen(nap),si.nap);
String so = String(n);
delete [] n;
return so;
}
// Not necessary. Since String(const char *) exists
// String String:: operator+(const char* c) const{
// return String(nap+*c);
// }
String operator+(const char* c,const String & s){
return String(String(s)+String(c));
}
std::ostream &operator<<(std::ostream& os,const String& s){
os<<s.nap<<std::endl;
return os;
}