我想重新學習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