Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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;
}
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 questionAsk a Question
731 491 924 answers to any question