0

In C++,How can I declare an array of objects with a compulsory string type argument(for parametrized constructor) of a certain size where size will be provided by user at runtime? Suppose 'Vertex' is my class which requires a string type argument for passing to parameterized constructor while declaring the object of 'Vertex'. When I write

Vertex s=Vertex("xx");

it is OK for declaring a single object. But I want the array size to be of N,which will be obtained from user at runtime.

In a nutshell,I want to have Vertex obj[N].What is the syntax for achieving that?

** I want my array of objects with a compulsory string argument to be passed for the parametrized constructor of my class.The said duplicate linked question doesn't have that criteria.**

  • 1
    `Vertex* vArr = new Vertex[N]` or use `std::vector::resize(n)` where N is given by the user. you class must have default c.tor – David Haim Jun 11 '15 at 16:05
  • By reading the first few chapters of your C++ book. – Lightness Races in Orbit Jun 11 '15 at 16:15
  • @ David Vertex* vArr = new Vertex[N] doesn't work as it requires a string argument too in the syntax.See my example. – starter.pack Jun 11 '15 at 16:19
  • @Lightness You din't even understand my question and marked it as duplicate.I wanted my array of objects with a compulsory string argument to be passed for the parametrized constructor of my class.Your linked question doesn't have that criteria. – starter.pack Jun 11 '15 at 16:28
  • @starter.pack: I admit I didn't look too hard for a matching duplicate, because there are millions on the topic and you didn't look too hard for any information. My effort is proportional to yours. – Lightness Races in Orbit Jun 11 '15 at 16:41
  • @LightnessRacesinOrbit Ohh yeah.Lame excuse for not answering(or knowing!) the actual question,rather wasting time. – starter.pack Jun 11 '15 at 19:41
  • @starter.pack: Anyone who has spent more than ten minutes in industry knows the answer. I have been a C++ professional for more like ten years. But, indeed, I choose not to answer obvious duplicates that have previously been asked many, many times. You are expected to put in some basic research effort, rather than wasting _our_ time. Remember, _you're_ the one asking for free help, not me. – Lightness Races in Orbit Jun 11 '15 at 19:53
  • @LightnessRacesinOrbit If you were so professional and knowledgeable,you would have answered without chest thumping for such a long period! and I din't ask for your help,you came here by yourself just to deviate the motive of the original discussion.You could have avoided the question simply.I wonder that is how you've spent your 10 years! – starter.pack Jun 11 '15 at 20:28

1 Answers1

1

You can use a std::vector in place of array, it will grow as it needed.

 std::vector<Vertex > vec;

If user give huge number it is a good option to reserve the allocation for the vector to avoid further realloaction of memory for vector using std::vector::reserve()

So if user input a huge number N,

 vec.reserve(N)
Steephen
  • 14,645
  • 7
  • 40
  • 47
  • Isn't it possible with array as I had other operations based on object index of the class? – starter.pack Jun 11 '15 at 16:22
  • @starter.pack you can pass string object as parameter to constructor. No one argue against it. If you prefer to use object as index, I think you are leading into a pair and key as object and value is corresponding value. For your above question, my answer is using std::vector also you can access elements by using index operator like array. In case of array you have to tediously manage the memory but if you use vector it will take care it. – Steephen Jun 11 '15 at 16:37