将字符串重新定义为类
Redefining String as class
我必须将字符串重新定义为类,并且在运算符+重载或复制构造函数时遇到问题。我的main()编译,但没有给出任何回报或涂鸦。这是类字符串的片段:
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;
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;
}
在你的运算符+中调用strcat(或更好的strncat),而不是添加指针。或者通过将一个小睡的字节复制到另一个小睡的末尾来自己做。在这两种情况下,您都必须确保分配了足够的内存!
加法运算符对我来说看起来不正确。
*
运算符可以读作 的内容。所以*s.nap
实际上是s.nap
的内容,这是一个char
,表示nap
指向的第一个字符。所以nap+*s.nap
不是你想要的,nap+*c
也不是.
您还需要类的析构函数,以确保删除nap
指向的内存。
相关文章:
- 我无法在 qt 的C++代码中定义字符串
- 无法在模板参数中定义字符串文本
- 如何使用运算符>>在自定义字符串中输入多个单词?
- llvm,通过 C++ API 定义字符串和数组
- 字符串流中的自定义字符串输入
- 如何在Visual Studio Code tasks.json中定义C++字符串宏?
- C++ 使用链表的自定义字符串类中的重载 + 运算符
- 如何在 C++ 中安全地为 char *array 重新分配内存(它适用于自定义字符串类)
- 我可以在"setDefaultButton"中使用我自己的用户定义字符串吗
- 在编译器选项上定义字符串
- Xcode 4.5.2 libc++ std::bad_cast 实现自定义(字符串)流时
- Visual C Inliner组装程序并定义字符串
- 自定义字符串类实现建议
- 自定义字符串类中的常量字符*差异
- 如何在C++中重新定义字符串的 = 运算符
- 定义字符串时C++许多错误
- 如何在C++头文件中定义字符串参数-用Swift桥接
- 如何用C++递归地从二进制文件中读取自定义字符串
- 自定义字符串排序
- 带有自定义字符串比较器的字符串的priority_queue的C++向量