0

I have a problem in C++ and i can't find answer for it. I have some classes:

class Xyz{
   //...
};
class MyClass1: public Xyz{
   //...
};
class MyClass2: public Xyz{
   //...
};
class MyClass3: public Xyz{
   //...
};

As you can see MyClass1, MyClass2 and MyClass3 have the same base class. Now i want to define some objects:

MyClass1 *object1 = new MyClass1();
MyClass2 *object2 = new MyClass2();
MyClass3 *object3 = new MyClass3();

After that i want to create an dynamic array with pointers at those objects (as i know it's possible) and an integer which save the number of elements in that array. I named that array someName and in future it should store object1, object2 and object3.

Xyz **someName;
int numberOfElements = 0;

Problems start here. someName is empty so i want to add something to it. But i don't know how to make an new array with given number of places for pointers to copy old element to it. This don't work ofcourse:

Xyz **someNameTemp = new Xyz[numberOfElements+1];

Any idea how the resizing function should look? Thanks for help.

4
  • ¤ You don't have an array. The statement "someName is empty" is false. So it's meaningless to talk about resizing. This declares and initializes a suitable array: Xyz* array[] = {object1, object2, object3};. It cannot be resized. By the way, whoever is declaring the standard library as off-limits to you, is not a good teacher of this subject. Consider studying on your own. Cheers & hth., Commented Dec 18, 2011 at 0:18
  • But how about creating a new temporary array, rewriting all objects and then switching pointers? Commented Dec 18, 2011 at 0:22
  • 1
    @AlfP.Steinbach, I disagree - it's important to understand both the basics as well as the STL. Learning only one or the other isn't a good idea, but you have to learn one before the other in the end, and learning the fundamentals first is a perfectly good idea. Commented Dec 18, 2011 at 0:46
  • @bdonlan: well you have a lot to learn. there's nothing inherently wrong in your statement that learning the fundamentals first is a good idea, but that statement is not in conflict with learning at a reasonable pace and without unreasonable struggles. not using the standard library is however in conflict with that, as you may learn. anyway, don't take my word for it. get yourself a good book (that doesn't make you needlessly struggle with details), such e.g. Koenig & Moo's "Accelerated C++", or see the C++ book list FAQ here on SO. cheers & hth., Commented Dec 18, 2011 at 8:36

2 Answers 2

2

You could avoid all the hassle, and just use a std::vector:

std::vector<Xyz *> pointers;

pointers.push_back(new MyClass1());
pointers.push_back(new MyClass2());
// etc.

[Side-note: You should strongly consider not using raw pointers; instead use smart pointers, as it makes resource-management far simpler.]

Sign up to request clarification or add additional context in comments.

Comments

0
Xyz **someNameTemp = new Xyz*[numberOfElements+1];
for (int i=0; i<numberOfElements; ++i)
    someNameTemp[i] = someName[i];
delete [] someName;
someName = someNameTemp;
++numberOfElements

// someName[numberOfElements - 1] uninitialized

Also, when you first create the pointer, be sure to initialize it. Either to NULL, or an allocated array.

1 Comment

proceeding in this fashion (to add new elements) yields square time, O(n^2). it's imho not a good idea to teach beginners such impractical techniques. it's like teaching a novice driver to refuel the car by demounting the fuel tank: yes it's technically possible, but.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.