OLD | NEW |
1 // Copyright 2005, Google Inc. | 1 // Copyright 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 // A sample program demonstrating using Google C++ testing framework. | 30 // A sample program demonstrating using Google C++ testing framework. |
31 // | 31 // |
32 // Author: wan@google.com (Zhanyong Wan) | 32 // Author: wan@google.com (Zhanyong Wan) |
33 | 33 |
34 #ifndef GTEST_SAMPLES_SAMPLE2_H_ | 34 #ifndef GTEST_SAMPLES_SAMPLE2_H_ |
35 #define GTEST_SAMPLES_SAMPLE2_H_ | 35 #define GTEST_SAMPLES_SAMPLE2_H_ |
36 | 36 |
37 #include <string.h> | 37 #include <string.h> |
38 | 38 |
39 | |
40 // A simple string class. | 39 // A simple string class. |
41 class MyString { | 40 class MyString { |
42 private: | 41 private: |
43 const char* c_string_; | 42 const char* c_string_; |
44 const MyString& operator=(const MyString& rhs); | 43 const MyString& operator=(const MyString& rhs); |
45 | 44 |
46 public: | 45 public: |
47 // Clones a 0-terminated C string, allocating memory using new. | 46 // Clones a 0-terminated C string, allocating memory using new. |
48 static const char* CloneCString(const char* a_c_string); | 47 static const char* CloneCString(const char* a_c_string); |
49 | 48 |
50 //////////////////////////////////////////////////////////// | 49 //////////////////////////////////////////////////////////// |
51 // | 50 // |
52 // C'tors | 51 // C'tors |
53 | 52 |
54 // The default c'tor constructs a NULL string. | 53 // The default c'tor constructs a NULL string. |
55 MyString() : c_string_(NULL) {} | 54 MyString() : c_string_(NULL) {} |
56 | 55 |
57 // Constructs a MyString by cloning a 0-terminated C string. | 56 // Constructs a MyString by cloning a 0-terminated C string. |
58 explicit MyString(const char* a_c_string) : c_string_(NULL) { | 57 explicit MyString(const char* a_c_string) : c_string_(NULL) { |
59 Set(a_c_string); | 58 Set(a_c_string); |
60 } | 59 } |
61 | 60 |
62 // Copy c'tor | 61 // Copy c'tor |
63 MyString(const MyString& string) : c_string_(NULL) { | 62 MyString(const MyString& string) : c_string_(NULL) { Set(string.c_string_); } |
64 Set(string.c_string_); | |
65 } | |
66 | 63 |
67 //////////////////////////////////////////////////////////// | 64 //////////////////////////////////////////////////////////// |
68 // | 65 // |
69 // D'tor. MyString is intended to be a final class, so the d'tor | 66 // D'tor. MyString is intended to be a final class, so the d'tor |
70 // doesn't need to be virtual. | 67 // doesn't need to be virtual. |
71 ~MyString() { delete[] c_string_; } | 68 ~MyString() { delete[] c_string_; } |
72 | 69 |
73 // Gets the 0-terminated C string this MyString object represents. | 70 // Gets the 0-terminated C string this MyString object represents. |
74 const char* c_string() const { return c_string_; } | 71 const char* c_string() const { return c_string_; } |
75 | 72 |
76 size_t Length() const { | 73 size_t Length() const { return c_string_ == NULL ? 0 : strlen(c_string_); } |
77 return c_string_ == NULL ? 0 : strlen(c_string_); | |
78 } | |
79 | 74 |
80 // Sets the 0-terminated C string this MyString object represents. | 75 // Sets the 0-terminated C string this MyString object represents. |
81 void Set(const char* c_string); | 76 void Set(const char* c_string); |
82 }; | 77 }; |
83 | 78 |
84 | |
85 #endif // GTEST_SAMPLES_SAMPLE2_H_ | 79 #endif // GTEST_SAMPLES_SAMPLE2_H_ |
OLD | NEW |