As @aaronman said, it would be no good for you if we just gave you the final solution: if you do want to learn something, you'd better try this on your own from scratch (otherwise just use existing std::vector or QVector or whatever vector).
So instead I would suggest you to see some tutorials or some exapmles for inspiration, and then start from scratch. Even simple google search for c++ tutorial implementing dynamic array will give some tolerable videos like this as well as some text links.
After you are clear with the implementation details, you might want to design the interface of your array, and the points to consider may be as following:
- Make this a
class instead of struct to make the fields be private by default (encapsulation)
- Implement initial allocation in constructor and de-allocation in desctuctor (see @juanchopanza's comment). Copy constructor and assignment operator are also good, but IMHO can be omitted at this point (e.g., you can make them private without implementation to simply prohibit copying)
- Implement all your functionality as methods, for example, a method
push_back might add to end. You will see that adding element can cause reallocation, so it would be a good idea to extract reallocation process into another (possibly private) method (to make it reusable) and implement it using ideas from that video, then call it from push_back if size == capacity.
- Learn about operator overloading, and implement operator[] to have access to elements just like in normal array.
- ...Continue adding functionality until you enjoy your implementation :) I think it is important when learning to have enjoyment from creating something neat.
Finally you can compare your implementation with std::vector or some other existing dynamic array to see if you are missing something. Some three years ago I had to make own implementation of vector in my diploma work (for fine-grained control of its modification and reallocation for performance reasons), you might also check it out, but (disclaimer!) you might find it kinda complicated and messy after all my attempts of optimization :)
Go ahead! Hope this helps (and inspires you a bit).
std::vectorcontentspoint to it. Then you would implement the copy constructor, assignment operator and destructor (look up "the rule of three". Once you have that up and running, you can start thinking of the expanding part.