create X objects

How can I create x objects of a class if x is an inputed int?
1
2
3
4
5
6
7
8
9
class test{
};


int x;
cout<<"Enter obj. numbers to create:";
cin>>x;
// create x objects of class test

How can I do this the easiest way ?
As x isn't a constant. Use the new keyword:
test *myTestArray = new test[ x ];

Just always remember to clean up afterwards:
delete[] myTestArray;
When you have time and if your instructor allows it at this time, you should consider learning how to use std::vector.
My instructor is a book and the internet , but thank you for the advice :D
If it's a good book, vectors are discussed long before new[], so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>

class test {
};

int main()
{
    int x;
    std::cout << "Enter obj. numbers to create:";
    std::cin >> x;

    std::vector<test> v(x); // create x objects of class test
}
Thanks for the advice Cubbi,but i'm confused:What should I use ,vectors or dynamic memory ? Is one of them better ? Are there any pros and cons ?
I think for your case if you know how many objects you are creating at start just use an array. Vectors are more needed if you need to add things to it as you go.
It's very important to know how to use the C++ Standard Library; that includes std::vector. You should learn std::vector first, and then learn manual dynamic memory management later.
closed account (3qX21hU5)
Vectors are more needed if you need to add things to it as you go.


Vectors are just as fast usually if not faster then arrays are, and they provide much more functionality then arrays do so why would you resort to the inferior tool for the job?


Is one of them better ? Are there any pros and cons ?

In my opinion you should almost always use vectors instead of arrays. The only exception I can think of is where performance testings shows arrays preform better and you need that extra speed, though like I stated above usually that is not the case.

The pros for vectors is that you can add onto them without having to resize them, they also have fast random access. They are also much much easier to use then arrays because of the extra features that are available.

The cons for vectors are that they can be slow when when deleting or inserting into the middle of the vector. You won't notice to much of a speed difference when dealing with vectors that have a small amount of elements. But when you have a vector that is really large (10,000 elements) and you have to insert or delete stuff not on the end of a vector it will get really slow. When you need to do something like this though you can use a list which is optimized for fast insertion/deletion.

That is not to say that you should not learn how arrays work because a lot of older programs were built using arrays so you will have to work with them. But generally if you have the choice go with vectors or other types of containers in the STL (lists, deques, ect.).
Last edited on
Dynamic allocation (new/delete) can get very complicated very quickly. The STL classes (including std::vector<>) will provide an easy wrapper for this. They do all of the low-level work for you so that the amount of code you need to write is limited, and there is less chance for you to make mistakes. Therefore, if you can use an STL container, use it!
Last edited on
Topic archived. No new replies allowed.