Thinking_in_C___2.rar
源代码在线查看: strings.h
//: C13:Strings.h
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Simple string class
// Can only be built on the heap
#ifndef STRINGS_H_
#define STRINGS_H_
#include
#include
class String {
char* s;
String(const char* S) {
s = new char[strlen(S) + 1];
std::strcpy(s, S);
}
// Prevent copying:
String(const String&);
void operator=(String&);
public:
// Only make Strings on the heap:
friend String* makeString(const char* S) {
return new String(S);
}
// Alternate approach:
static String* make(const char* S) {
return new String(S);
}
~String() { delete s; }
operator char*() const { return s;}
char* str() const { return s; }
friend std::ostream&
operator return os }
};
#endif // STRINGS_H_ ///:~