-1

I'm new in C++; here is my problem.
DataInterface is a struct defined.
I would like to create Datainterface_1, Datainterface_2, Datainterface_3 as an instance of existing DataInterface. How to do that.
I wrote the following :

for (int i = 0; i < 3; ++i)
{
    DataInterface* Datainterface_$i = .....;
}

It doesn't work. Any ideas?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • 3
    Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here.It's specially important to post a [mcve]. – R Sahu Jun 21 '18 at 16:18
  • 2
    It would be worthwhile for you to check out a few basics on C++, for example these [books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Max Langhof Jun 21 '18 at 16:28
  • 1
    You are basically asking for a tutorial in C++, in order to get some of those script language ideas sorted out. Finding one, toying with a HelloWorld and working upwards toward containers (vectors etc.) is a good idea. But asking this here is off-topic. – Yunnosch Jun 21 '18 at 16:38

1 Answers1

0

Use a container - like std::vector or std::array - to hold multiple instances.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • 1
    Using a container really is the simplest solution to this problem, I don't understand the downvote. Though `std::array` might be better suited. – François Andrieux Jun 21 '18 at 16:39
  • I did not downvote, but the question as asked wants to create multiple different variables. It's a pretty obvious XY problem, but as the asker is evidently unfamiliar with very basic C++ concepts, there is no answer short of a comprehensive tutorial that will actually help them. – Max Langhof Jun 21 '18 at 16:43
  • 2
    @Max Langhof - Sure, it does ask that, but that's *most likely* not the right solution (as you say; [XY problem](http://xyproblem.info) and lack of C++ knowledge). But, the question also *does* ask for "Any idea" ;) Any way, I'll leave this answer as it is - regardless of downvotes. – Jesper Juhl Jun 21 '18 at 16:51
  • why not doing: DataInterface * Datainterface = new DataInterface[3] followed by for (int i=0; i< 3; ++i) Datainterface[i] = ......; what do you think – handy emmanuel Jun 21 '18 at 17:03
  • @handy emmanuel The simple fact that that uses manual memory management makes me go "No, please don't do that. Use containers and/or smart pointers". – Jesper Juhl Jun 21 '18 at 17:08
  • why no? with your method, I will need to define 3 array and populate each one. – handy emmanuel Jun 21 '18 at 17:37
  • @handy emmanuel manual memory management is error prone. You have to remember `delete` all the right places (also in case of exceptions) and you have to keep track of *who* is in charge of deleting a resource. Letting a container and/or smart pointer (like `std::unique_ptr`/`std::shared_ptr`) track lifetime and ownership for you is much less error prone. Why would tou need to define 3 arrays? One array holding `n` instances would be enough. – Jesper Juhl Jun 21 '18 at 17:58
  • please could you write how you could use std::array to do what I was asking. Thanks – handy emmanuel Jun 21 '18 at 23:29
  • @handy emmanuel something along the lines of `std::array interfaces; for (auto& elem : interfaces) { elem = new DataInterface(...); }` or similar. Then use `interfaces[0 through 2]`. You probably don't want raw pointers and manual memory management though - better use smart pointers. – Jesper Juhl Jun 27 '18 at 19:24