Consider the method
functionA (double[] arg)
I want to pass a double inline array like
functionA({1.9,2.8})
instead of creating an array first and then passing it, e.g.
double var[] = {1.0,2.0}; functionA(var);
Is this possible with C ++? It sounds simple, but I could not find a hint of my question, which made me suspicious :).
You can do this with std::initializer_list<>
std::initializer_list<>
#include<vector> void foo(const std::initializer_list<double>& d) { } int main() { foo({1.0, 2.0}); return 0; }
Which compiles and works for me under g ++ with -std=c++0x .
-std=c++0x
This works with C ++ 0x
void functionA(double* arg){ //functionA } int main(){ functionA(new double[2]{1.0, 2.0}); //other code return 0; }
Although you need to make sure that the memory allocated by the new one is deleted in the A () function, otherwise a memory leak will occur!
You can do this in C ++ 11 using std::initializer_list .
std::initializer_list
void fun(std::initializer_list<double>); // ... fun({ 1., 2. });
You cannot do this in C ++ 03 (or it will include a sufficient template, this will not be possible).
Source: https://habr.com/ru/post/905173/More articles:How to use navigator.app.exitApp () to exit the application in a phone gap? - cordovaClose jQuery Android Application - Phonebook - jqueryHow to find record line number? - sqlShortest way to open encoded file and readLine () in Java? - javaHow to effectively remove word, colon and comma in VIM - vimUsing the ClassPa class, thanksmlApplicationContext in a stand-alone Java class - javapreventing duplication of infinite scroll ajax loader - jqueryInteraction with a Powershell process invoked from a Java application - javaExporting a SQL Server stored procedure to an Excel workbook with multiple worksheets - sql-serverHow to convert string data to a JSON object in python? - jsonAll Articles