class MyString { private: const char * c_string_; const MyString& operator=(const MyString& rhs); public: // Clones a 0-terminated C string, allocating memory using new. static const char * CloneCString(const char * c_string); //////////////////////////////////////////////////////////// // // C'tors // The default c'tor constructs a NULL string. MyString() : c_string_(NULL) {} // Constructs a MyString by cloning a 0-terminated C string. explicit MyString(const char * c_string) : c_string_(NULL) { Set(c_string); } // Copy c'tor MyString(const MyString& string) : c_string_(NULL) { Set(string.c_string_); } //////////////////////////////////////////////////////////// // // D'tor. MyString is intended to be a final class, so the d'tor // doesn't need to be virtual. ~MyString() { delete[] c_string_; } // Gets the 0-terminated C string this MyString object represents. const char * c_string() const { return c_string_; } size_t Length() const { return c_string_ == NULL ? 0 : strlen(c_string_); } // Sets the 0-terminated C string this MyString object represents. void Set(const char * c_string); }; |
原帖由 aaaaal 于 2009-7-28 17:25 发表
MyString(const MyString& string) : c_string_(NULL) {
Set(string.c_string_);
}
这里是通过对象访问的吧?
string是某个MyString的对象的引用吧?
A member of a class can be
— private; that is, its name can be used only by members and friends of the class in which it is
declared.
— protected; that is, its name can be used only by members and friends of the class in which it is
declared, and by members and friends of classes derived from this class (see 11.5).
— public; that is, its name can be used anywhere without access restriction.
15.3 Access Control [hier.access]
A member of a class can be p r i v a t e , p r o t e c t e d , or p u b l i c :
– If it is p r i v a t e , its name can be used only by member functions and friends of the class in
which it is declared.
– If it is p r o t e c t e d , its name can be used only by member functions and friends of the class in
which it is declared and by member functions and friends of classes derived from this class
(see §11.5).
– If it is p u b l i c , its name can be used by any function.
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |