now the non-const String::operator[] returns a Char class instead of a

primitive
This commit is contained in:
Kjistóf 2016-10-23 15:29:06 +02:00
parent 44ef56941e
commit 4a1709d626
3 changed files with 8 additions and 4 deletions

View File

@ -10,5 +10,5 @@ find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS}) include_directories(${GTEST_INCLUDE_DIRS})
link_libraries(${GTEST_BOTH_LIBRARIES}) 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}) add_executable(cpp11NHF1_refcountedString ${SOURCE_FILES})

View File

@ -1,4 +1,5 @@
#include "String.h" #include "String.h"
#include "Char.h"
#include <cstring> #include <cstring>
#include <memory> #include <memory>
@ -114,9 +115,9 @@ const char& String::operator[](size_t index) const
} }
// reuse const operator[] // reuse const operator[]
char& String::operator[](size_t index) Char String::operator[](size_t index)
{ {
return const_cast<char&>(const_cast<const String*>(this)->operator[](index)); return Char(_str->index_of(&const_cast<char&>(const_cast<const String*>(this)->operator[](index))), *this);
} }
size_t String::size() const size_t String::size() const

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include "StringValue.h" #include "StringValue.h"
#include "Char.h"
class Char;
@ -30,12 +32,13 @@ public:
String& operator+=(char); String& operator+=(char);
bool operator==(const String&) const; bool operator==(const String&) const;
const char& operator[](size_t) const; const char& operator[](size_t) const;
char& operator[](size_t); Char operator[](size_t);
size_t size() const; // does not include null-terminator size_t size() const; // does not include null-terminator
const char* c_str() const; const char* c_str() const;
friend std::istream& operator>>(std::istream&, String&); friend std::istream& operator>>(std::istream&, String&);
friend class Char;
}; };
std::ostream& operator<<(std::ostream&, const String&); std::ostream& operator<<(std::ostream&, const String&);