- #1
yungman
- 5,755
- 293
Hi
I have been literally studying back class overloading in the new C++ syntax( at least that's what I call). This is on class template that is very much like class. I want to verify whether my translation is correct or not. In the program, I have the constructor and operator[] with throw catch written in new form according to the Ivor book. Then following those with my translation to the old style syntax. Can anyone check whether I am right?
I am quite sure the constructor is correct, it's the try throw catch part that I have question in line 24, I don't understand + to_string(index). I just use my best guess in my translation to the old syntax.
My other question, which is more important:
If those are "new" and "better" syntax is supposed to simplify the program, how is it better? From here, I have been told to make the program easier to read and understand by other people, that I even have to use names that are easier to understand...In what world the new syntax are easier to understand? In what world the new syntax are shorter and simpler?
At least the old way of writing is more English, that it's easier to understand.
I know I have NO CHOICE but to stop and relearn this, it's like take it or leave it. I just wonder what you guys think.
Thanks
I have been literally studying back class overloading in the new C++ syntax( at least that's what I call). This is on class template that is very much like class. I want to verify whether my translation is correct or not. In the program, I have the constructor and operator[] with throw catch written in new form according to the Ivor book. Then following those with my translation to the old style syntax. Can anyone check whether I am right?
I am quite sure the constructor is correct, it's the try throw catch part that I have question in line 24, I don't understand + to_string(index). I just use my best guess in my translation to the old syntax.
C++:
//Ivor page 617
#include<iostream>
using namespace std;
template<typename T> class Array
{
private: T* elements; size_t size;
public:
explicit Array<T>(size_t aSize) //Constructor
: elements{ new T[aSize] }, size{ aSize }{}
Array<T>(const Array<T>& array) : Array{ array.size }
{for (size_t i{}; i < size; ++i)elements[i] = array.elements[i];}
/*Constructor written in old syntax:
explicit Array<T>(int aSize)
{
element=new T[aSize];size=aSize;
for(int i=0;i<size;I++)elements[i]=array.elements[i];
}*/
~Array<T>() { delete[] elements; }
T& operator[](size_t index) const
{
if (index >= size)
throw out_of_range{" Index too large: "+ to_string(index)};//No idea what is this
return elements[index];
}
/*Written in old syntax:
T& operator[](int index) const
{
try
{
if(index>=size)throw 0;
return elements[index];
}
catch(int i){ cout << " Index too large.\n\n";}
}*/
Array<T>& operator = (const Array<T>& rhs)
{
if (&rhs != this)
{ delete[] elements;//why have this?
size = rhs.size;
elements = new T[size];
for (size_t i{}; i < size; ++i)
{elements[i] = rhs.elements[i];}
}
return *this;
}
size_t getSize() const { return size; }
};
My other question, which is more important:
If those are "new" and "better" syntax is supposed to simplify the program, how is it better? From here, I have been told to make the program easier to read and understand by other people, that I even have to use names that are easier to understand...In what world the new syntax are easier to understand? In what world the new syntax are shorter and simpler?
At least the old way of writing is more English, that it's easier to understand.
I know I have NO CHOICE but to stop and relearn this, it's like take it or leave it. I just wonder what you guys think.
Thanks