我想重新学习C++,并想知道是否有人可以帮我在这里。我试图实现我自己的String类来查看我是否可以记住事情,但我被卡在构造函数中。C++:如何实现自己的String类?
我有我的头文件,并希望有一个构造函数这样:
头文件(MYFILES \ STRING.H):
#ifndef STRING_
#define STRING_
using namespace std;
#include <iostream>
class String
{
private:
static const unsigned int MAX = 32; // Capacity of string
char Mem[MAX]; // Memory to hold characters in string
unsigned Len; // Number of characters in string
public:
// Construct empty string
//
String()
{
Len = 0;
}
// Reset string to empty
//
void reset()
{
Len = 0;
}
// Return status information
//
bool empty() const
{
return Len == 0;
}
unsigned length() const
{
return Len;
}
// Return reference to element I
//
char& operator[](unsigned I)
{
return Mem[I];
}
// Return constant reference to element I
//
const char& operator[](unsigned I) const
{
return Mem[I];
}
// Construct string by copying existing string
//
String(const String&);
// Construct string by copying array of characters
//
String(const char []);
// Copy string to the current string
//
String& operator=(const String&);
// Append string to the current string
//
String& operator+=(const String&);
};
// Compare two strings
//
bool operator==(const String&, const String&);
bool operator!=(const String&, const String&);
// Put a string into an output stream
//
ostream& operator<<(ostream&, const String&);
#endif
我卡上的位是这样的:
String::String(const String& str)
{
//what goes here?
}
谢谢!
世界真的是没有另一个字符串类更好... – 2011-03-16 23:21:27
@Kornel:演习的重点是学习,而不是取代的std :: string。 – 2011-03-16 23:24:30