So, I've been busy studying other subjects preparing for entrance exams and haven't had much time to code lately but I found some time yesterday and added a little more content to my String class. I'm having trouble overloading the >> operator for input. Any help suggesting how I should approach this will be great!
Here's the main issue part...
Code:
std::istream& operator >> (std::istream& stream , String& _String_)
{
_String_.m_Size = 0;
delete [] _String_.m_Text;
char* Input;
stream >> Input;
_String_ = String((const char*)Input);
return stream;
}
[EDIT]
Also, I'm creating a new string when _String_ = String((const char*)) happens. Should I be doing that or should I write an overload = for const char*? I haven't done that because a statement like A = "Hello World" (where A is a String instance) works correct as it basically is the same as A = String("Hello World").
[/EDIT]
Now, obviously I cannot do stream >> Input because Input is just a pointer to a char and has no size bound to it. I can get around this by setting a max size to it (say 256) but I don't want to do that. How would you guys suggest taking care of the input?
Here's the entire code till now...
Code:
class String
{
private: /* Variables */
size_t m_Size;
char* m_Text;
private: /* Method Declarations and/or Definitions */
size_t StringLength (const char* _String_) const;
void StringCopy (const char* _String_);
public: /* Constructors and Destructor */
String ();
String (const char* _String_);
String (const String& _String_);
~String ();
public: /* Getters and Setters */
const size_t& Size (void) const;
char& At (signed int Position) const;
public: /* Method Declarations and/or Definitions */
friend std::ostream& operator << (std::ostream& stream , const String& _String_);
friend std::istream& operator >> (std::istream& stream , String& _String_);
String& operator = (const String& _String_);
char& operator [] (signed int Position) const;
};
/* Private: Method Declarations and/or Definitions */
size_t String::StringLength (const char* _String_) const
{
size_t Position = 0;
while (_String_[Position] != '\0') Position++;
return Position;
}
void String::StringCopy (const char* _String_)
{
size_t Position = 0;
while (Position < m_Size + 1)
{
m_Text[Position] = _String_[Position];
Position++;
}
}
/* Public: Constructors and Destructor */
String::String ()
: m_Size(0) ,
m_Text(nullptr)
{ }
String::String (const char* _String_)
: m_Size(StringLength(_String_)) ,
m_Text(new char [m_Size + 1])
{
StringCopy(_String_);
}
String::String (const String& _String_)
: m_Size(_String_.Size()) ,
m_Text(new char [m_Size + 1])
{
StringCopy(_String_.m_Text);
}
String::~String ()
{
delete [] m_Text;
}
/* Public: Getters and Setters */
const size_t& String::Size (void) const
{
return m_Size;
}
char& String::At (signed int Position = 0) const
{
/*
String: "Hello"
Size: 5
Indexing: 'H' 'E' 'L' 'L' 'O' '\0'
0 1 2 3 4 5
-5 -4 -3 -2 -1
*/
return operator [] (Position);
}
/* Public: Method Declarations and/or Definitions */
std::ostream& operator << (std::ostream& stream , const String& _String_)
{
return stream << _String_.m_Text;
}
std::istream& operator >> (std::istream& stream , String& _String_)
{
_String_.m_Size = 0;
delete [] _String_.m_Text;
char* Input;
stream >> Input;
_String_ = String((const char*)Input);
return stream;
}
char& String::operator [] (signed int Position) const
{
/*
String: "Hello"
Size: 5
Indexing: 'H' 'E' 'L' 'L' 'O' '\0'
0 1 2 3 4 5
-5 -4 -3 -2 -1
*/
if (Position < 0)
return ((Position > -(int)m_Size) ? m_Text[(int)m_Size + Position] : m_Text[0]);
return ((Position < (int)m_Size) ? m_Text[Position] : m_Text[(int)m_Size - 1]);
}
String& String::operator = (const String& _String_)
{
delete [] m_Text;
m_Size = _String_.m_Size;
m_Text = new char [m_Size + 1];
StringCopy(_String_.m_Text);
return *this;
}