kind of finding hard to adapt to references... pointers have the special value NULL that helps me to check if an object is present or unavailable. So next question would be if the new[] operator initialises all pointers to the NULL value?
This is an array of 10 int pointers. They are NOT set to NULL by default.
To do it dynamically you write:
int** ptrArray = new int*[10];
In this case, the pointers are set to NULL;
It's really easy to understand this stuff by just playing around. Just declare them and cout them. If you see an address, you know it's not set to NULL.
@framework/wazzak wow! I am pretty baffled that it is possible to have something like that in C++. I supposed that references were no more than aliases for the real variable. But thinking about, what would happen if you create an array of references? For instance I know that (at least in C) if you create an array of 10 integers, you are allocating a contiguous memory block that has enough space to hold 10 integers. Now, if you make an array of references of integers, I presume that the only manner to keep a contiguous memory block is through the use of pointers, and therefore the way you access items in your code is merely syntactic sugar hidden by the compiler. In the end an array of pointers is exactly the same as an array of references. But I am confused what makes an array of pointers+references?
@IceThatJaw and andywestken I think your answers contrast, but if I understood well invoking the default constructor of a basic type like an integer or a pointer will always assign the value 0 ? and new[] invokes the empty constructor?
Dean, I am pretty green but I actually tested what I said.
Now, I am educated enough to know that when I got all zero's from int** ptrArray = newint*[10];, it could have been some garbage picked up from the heap.
Shit like that happens alot and makes debugging even more frustrating. I would probably take andywestken's word over mine.
And string is a C++ STL class so it is always set to logical null upon creation. Integers however are not unless they are defined outside of a class or struct, I believe.
Also, if you are declaring an array of objects such as classes, the default constructor must be available because it is the only constructor that can and will be called. ( you will get a compiler error if it isn't)