+ 3
Not The Same Array
This question https://www.sololearn.com/Discuss/752696/?ref=app gave me a weird idea! I'm coding this useless class: a new array type!!! This is my code: https://code.sololearn.com/cXlesHuLYJE9/?ref=app First target was what @SirazNaoren requested: a way to make the index value of the first element 1 instead of 0. Then I'm adding new useless features 😂😂😂. So if you got new ideas to be implemented, hints or something else feel free to comment.
13 Answers
+ 10
Your version log inspired me, Andrea.!
There is a lot to learn from them and the way you manage your codes.
+ 1
Thanks a lot @Babak :D
+ 1
The things I think about pretty much immediatly:
Allow us to initialize the array by initializer list:
Array<int> arr{ 3, 4, 5 };
Implement iterators so we can do stuff like this:
for( const auto& i : arr )
{
std::cout << i << " ";
}
arr = arr2 crashes, try to fix it or disallow copying ^^
Also you should use the constructor's initializer list
Array(int n)
:ptr( new T[n] ),
numberOfElements(n)
{}
and since you probably don't want negative array sizes, try unsigned or std::size_t
Happy coding :)
+ 1
@thanks Dennis for your ideas, let see what I can do.
Edit:
- constructor initializer list added
- arr = arr2 is now not allowed
+ 1
I guess your code isn't updated yet.
I implemented the things as well, just for fun.
You can get basic iterators with just 2 lines of code.
Btw, you should remove the if check inside the [] overload.
C++ is there for speed, not to hold hands for people who use the wrong index.
That is where the at function is for :)
0
Good. I never implemented an iterator nor variable list so I need to study.
If check into [] operator was written to allow different index logic.
0
@Dennis write here what is missing, so I can add now. I will learn about them.
0
Yea, sorry I miss interpreted the constructor initializer with taking input as initializer list, they kinda look the same so ^^.
Instead of putting the copy constructor and copy assignment operator inside private it is better to put them in public and declaring them as deleted;
public:
Array(const Array&) = delete;
...
For the taking input as an initializer list.
Check out
http://en.cppreference.com/w/cpp/utility/initializer_list
As for the iterator,
you use a function begin, which returns a pointer to the first element inside the array.
and a function end which points to the 1 past the last element inside the array.
If you want, I can give you the 2 lines. I don't want to post them yet in case you don't want to be spoiled.
0
@Dennis thanks.
Now in italy is time to go to bed. I change only copy constr & copy assign. Tomorrow I'll have a look for the other things.
Anyway post them if you want...i know how to create that functions, but I never created/used (yes also used I'm sorry ahahah) an iterator so it will be useful 4 me.
Edit: = deleted gave me errors.
0
Alright, here you go. ( time for me to go to bed as well :D )
T* begin(){ return ptr; }
T* end() { return ptr + numberOfElements; }
Just basic iterators ^^
0
@Dennis yes those are the functions but...ok i need to study what is an iterator...goodnight
0
It is delete, not deleted, gn
0
Ops...ahahah