|
-
April 23rd, 2010, 07:05 PM
#6
Re: ArrayList class
 Originally Posted by rkennedy9064
I just finished building an arraylist class in c++ and was wondering if i did the add method right. I did java programming before but am new with pointers.
There is a time and place for usage of pointers in a C++ program.
One of those times is when you are using polymorphism, where you need a pointer (or reference) to a parent class. The other is if you really do need an object to outlast the lifetime of the local scope where it's declared. Another reason to use a pointer is if the function you're calling requires a pointer (obviously).
All of the above scenarios occur in most non-trivial C++ programs, so you aren't going to miss the chance to use pointers.
However, trying to introduce yourself to pointers by writing container classes, is IMO not the way to go. There are already classes that do what your code is attempting to do, such as vector (which has already been mentioned), and they work already.
It is impossible for a beginner to write a container class correctly-- it isn't impossible for a beginner to actually start using the existing container classes correctly. Instead of wasting a lot of time writing container classes, learn to actually use the container classes that exist to your advantage, along with the various algorithms that you can use with the containers. Also, if you want to study pointers, study why pointer members (such as your class has) can be flawed. You don't need to physically code a vector class to learn the pitfalls of such classes.
I'll give you some advice -- every C++ interview I've taken part in (both interviewing and being interviewed) required extensive knowledge of the container classes and algorithms. Not one question asked me (or asked to the applicaant) required coding a vector class, linked list, etc., As a matter of fact, if you boasted about writing your own container classes, and you knew little or nothing about the standard containers and algorithms, that was a strike against you.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 23rd, 2010 at 07:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|