From 4a1709d626d082506437775f0a81ad5a7fac9f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 23 Oct 2016 15:29:06 +0200 Subject: [PATCH] now the non-const String::operator[] returns a Char class instead of a primitive --- CMakeLists.txt | 2 +- String.cpp | 5 +++-- String.h | 5 ++++- 3 files changed, 8 insertions(+), 4 deletions(-) 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