#Implementation
Implementation
#Design
Design
#Implementation
#Design
Implementation
Design
#Implementation
using namespace std;
is considered bad practice and should be avoided.- Naming: Many variable names tell nothing about their meaning. For example, what does
value2
do? How is it different fromvalue
? Better names enhance the readability of the code! - The
IntFile
constructor should probably take the path to the file as a parameter (there might be other files than "test.txt"). - The
IntFile
constructor never initializesIntFile::value
. IntFile::file
andIntFile::value
should probably be private.file
is never checked if it actually is a valid file handle.- Rule of 3/5 violation:
IntFile
acquires a resource and has a custom destructor. As such, the copy constructor, move constructor, copy assignment operator and move assignment operator should also be manually implemented or deleted (the compiler generated ones will very likely do the wrong thing). getValue
andsetValue
assume thatsizeof(int)
will always be 4 bytes. This is not the case! (e.g. if compiled for 64bit,sizeof(int) == 8
)getValue
andsetValue
aren't aware of endianess. This can mean that writing a value0x00000001
on one platform will be read as0x01000000
on another.setValue
s return value is always the same. Maybe its return type should be changed tovoid
?friend struct iterator
does not refer toIntFile::iterator
, but tostd::iterator
instead (reason: no forward declaration forIntFile::iterator
andusing namespace std;
combined with#include <vector>
).iterator::iterator(IntFile*, int, int)
never initializesiterator::value2
.iterator::iterator(IntFile*, int, int)
unnecessarily callsfseek
.iterator::iterator(int)
does not initializeiterator::ptr
anditerator::index
, thus setting every subsequent operation up for undefined behavior (e.g. dereferncingiterator::ptr
).iterator &iterator::operator=(const iterator&)
never assignsiterator::ptr
anditerator::index
.int &iterator::operator=(int)
never returns anint
value.iterator &iterator::operator++()
never returns aiterator&
.iterator &iterator::operator--()
never returns aiterator&
.iterator &iterator::operator+=(int)
never returns aiterator&
.- I can't imagine what
iterator::operator+(const iterator&)
oriterator::operator-(const iterator&)
would be used for. IntFile::end(int)
never checks ifNumber
is out of bounds (could be negative or too large).IntFile::rbegin()
andIntFile::rend(int)
should probably returnstd::reverse_iterator<iterator>
instead (currently iterating backwards has to be done with--iter
instead of the expected++iter
.Proxy::index
should be of the same type as the parameterindex
ofIntFile::setValue
andIntFile::getValue
to prevent slicing errors (sizeof(long)
might be greater thansizeof(int)
).- The call to print to
std::cout
inProxy::operator int()
is never reached.
#Design
IntFile::setValue
: I'd expect the first parameter to be the index and the second parameter to be the value).IntFile
could provide an overload foroperator[]
.
lang-cpp