diff --git a/CMakeLists.txt b/CMakeLists.txt index 56237d9..1247425 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,5 +10,5 @@ find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) link_libraries(${GTEST_BOTH_LIBRARIES}) -set(SOURCE_FILES String.cpp String.h StringValue.cpp StringValue.h test.cpp) +set(SOURCE_FILES String.cpp String.h StringValue.cpp StringValue.h Char.cpp Char.h test.cpp) add_executable(cpp11NHF1_refcountedString ${SOURCE_FILES}) \ No newline at end of file diff --git a/String.cpp b/String.cpp index 4cab9db..c0e3deb 100644 --- a/String.cpp +++ b/String.cpp @@ -1,4 +1,5 @@ #include "String.h" +#include "Char.h" #include #include @@ -114,9 +115,9 @@ const char& String::operator[](size_t index) const } // reuse const operator[] -char& String::operator[](size_t index) +Char String::operator[](size_t index) { - return const_cast(const_cast(this)->operator[](index)); + return Char(_str->index_of(&const_cast(const_cast(this)->operator[](index))), *this); } size_t String::size() const diff --git a/String.h b/String.h index 70b5981..75eab5f 100644 --- a/String.h +++ b/String.h @@ -1,6 +1,8 @@ #pragma once #include #include "StringValue.h" +#include "Char.h" +class Char; @@ -30,12 +32,13 @@ public: String& operator+=(char); bool operator==(const String&) const; const char& operator[](size_t) const; - char& operator[](size_t); + Char operator[](size_t); size_t size() const; // does not include null-terminator const char* c_str() const; friend std::istream& operator>>(std::istream&, String&); + friend class Char; }; std::ostream& operator<<(std::ostream&, const String&); \ No newline at end of file