S
S
sudo rm -rf /2017-09-15 14:35:34
C++ / C#
sudo rm -rf /, 2017-09-15 14:35:34

Is it possible in C++ to pass arrays to a function immediately upon declaration?

There is a custom class for arrays of some objects. For example, let there be strings.
I would like to be able to declare them like this: How can this be achieved and is it possible ?? UPD1 You need to write a curriculum, and set a couple of "wired" options, in addition to those that will be created in the course of work, in order to have some data right away. So I want to do something like:
Objarr var1({"Строка1", "Строка2", "Строка3"}, 3);

Objarr var1({"Строка1", "Строка2", "Строка3"}, 3);
Objarr var2({"Строка1", "Строка2"}, 2);
Objarr var3({"Строка1", "Строка2", "Строка3", "Строка4", "Строка5"}, 5);

to be able to quickly add / remove "wired" options. And isn't the curriculum a good reason to try something new?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
devalone, 2017-09-15
@devalone

It is possible like this:

#include <iostream>

void func(std::initializer_list<int> data)
{
  for(const auto& item : data) {
    std::cout << item << std::endl;
  }
}

int main() {
  func({1, 2, 3});
  std::cout << std::endl;
  func({1, 2, 3, 10, -1, 15});
  return 0;
}

D
Derevyanko Alexander, 2017-09-15
@dio4

use map and create map objects in the constructor. Then add the created objects to the array. Then you will use such an array in the class, passing it there to the constructor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question