Hey forum!
I am working on my own templatized dynamic array class. I understand a similar class is already provided in the HP standard template library--or at least it used to be. However, I thought it would be a useful learning experience to write one from scratch. I hoped doing so might help me become more confident with programming using templates and also as a general 'warm up' to ease me back in to C++ as a whole.
In order to make copy-construction and copy-assignment as efficient as possible I keep the actual array data itself within a dynamically allocated reference-counted structure. I then pass pointers to this and increment the reference count as necessary. As an aside; I am starting to suspect the new 'move' semantics introduced in the fantastic C++11 upgrade to the language make copy-on-change and reference counting an obsolete approach. Still that is a matter for its own question.
To prevent this structure being abused by any future end-users (although obviously in my case no one else will ever use this!) I declare it as a private member of my dynamic array class.
So, my declaration looks like this with most of the irrelevant parts elided:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
template<typename typeArray>
class TCDynamicArray {
public://Constructors and destructor.
TCDynamicArray();
...//Constructors that allow you to predefine the array size on declaration.
TCDynamicArray(const TCDynamicArray<typeArray> &tcArray);
TCDynamicArray(TCDynamicArray<typeArray> &&tcArray);
...//Conversion constructors that allow
//the array to copy its data from other statically allocated array types.
~TCDynamicArray();
public://Operators for 'copy' and 'move' assignment.
const TCDynamicArray<typeArray>& operator=(const TCDynamicArray<typeArray> &TcArray);
const TCDynamicArray<typeArray>& operator=(TCDynamicArray<typeArray> &&TcArray);
public://Public member functions.
...//Functions which allow a new element to be
//pushed onto/popped off the start or end of the array
private://Private structures.
template <typename typeArray>
struct TsARRAYCORE {
int m_iArrayLength;
int m_iIndexMaximum;
int m_iIndexNext;
int m_iReferenceCount;
typeArray *m_pTArray;
};
private://Private member functions.
...//Core allocation and deallocation functions primarily.
private://Private data members.
TCDynamicArray<typeArray>::TsARRAYCORE<typeArray> *m_pTsCore;
};
|
This works so far, so well in my testing at least. However every time I need to allocate a new instance of the array core structure I have to type in the very long
TCDynamicArray<typeArray>::TsARRAYCORE<typeArray>
declaration. This is both tedious and continually invutes typing mistakes...
I wonder if there is some dodge I can use to make this declaration simpler and less error-prone?
I considered trying
typedef sARRAYCORE TCDynamicArray<typeArray>::TsARRAYCORE<typeArray>
, but this gave me errors which I think were related to scope. I suppose a pre-processor macro would work, but that short circuits all the handy type-checking capabilities of VisualStudio intellisense and the compiler itself.
I would welcome any suggestions or comments.