Initialize object containing c-style array as member variable (c++)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












Consider following code:



struct Color //this struct can't be modified

double grey;
double rgb[3];
;

int main()

double myRGB[3] = 2, 6, 9;

Color c = 10, myRGB; //this line doesn't work

return 0;



How can I initialize a Color object in one line? In my real case scenario, Color struct can't be change (for example to use std::array instead of C-Style array).










share|improve this question



















  • 4




    Color c = 10, 2, 6, 19;?
    – Matthieu Brucher
    1 hour ago






  • 2




    If you must use a variable for the initialization, then I suggest you create a constructor for the Color structure.
    – Some programmer dude
    1 hour ago















up vote
6
down vote

favorite












Consider following code:



struct Color //this struct can't be modified

double grey;
double rgb[3];
;

int main()

double myRGB[3] = 2, 6, 9;

Color c = 10, myRGB; //this line doesn't work

return 0;



How can I initialize a Color object in one line? In my real case scenario, Color struct can't be change (for example to use std::array instead of C-Style array).










share|improve this question



















  • 4




    Color c = 10, 2, 6, 19;?
    – Matthieu Brucher
    1 hour ago






  • 2




    If you must use a variable for the initialization, then I suggest you create a constructor for the Color structure.
    – Some programmer dude
    1 hour ago













up vote
6
down vote

favorite









up vote
6
down vote

favorite











Consider following code:



struct Color //this struct can't be modified

double grey;
double rgb[3];
;

int main()

double myRGB[3] = 2, 6, 9;

Color c = 10, myRGB; //this line doesn't work

return 0;



How can I initialize a Color object in one line? In my real case scenario, Color struct can't be change (for example to use std::array instead of C-Style array).










share|improve this question















Consider following code:



struct Color //this struct can't be modified

double grey;
double rgb[3];
;

int main()

double myRGB[3] = 2, 6, 9;

Color c = 10, myRGB; //this line doesn't work

return 0;



How can I initialize a Color object in one line? In my real case scenario, Color struct can't be change (for example to use std::array instead of C-Style array).







c++ arrays constructor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









NathanOliver

81k15110169




81k15110169










asked 1 hour ago









Gaetan

1125




1125







  • 4




    Color c = 10, 2, 6, 19;?
    – Matthieu Brucher
    1 hour ago






  • 2




    If you must use a variable for the initialization, then I suggest you create a constructor for the Color structure.
    – Some programmer dude
    1 hour ago













  • 4




    Color c = 10, 2, 6, 19;?
    – Matthieu Brucher
    1 hour ago






  • 2




    If you must use a variable for the initialization, then I suggest you create a constructor for the Color structure.
    – Some programmer dude
    1 hour ago








4




4




Color c = 10, 2, 6, 19;?
– Matthieu Brucher
1 hour ago




Color c = 10, 2, 6, 19;?
– Matthieu Brucher
1 hour ago




2




2




If you must use a variable for the initialization, then I suggest you create a constructor for the Color structure.
– Some programmer dude
1 hour ago





If you must use a variable for the initialization, then I suggest you create a constructor for the Color structure.
– Some programmer dude
1 hour ago













1 Answer
1






active

oldest

votes

















up vote
10
down vote













Since Color is an aggregate you can use aggregate initialization and put the array initializer directly in the braces like



Color c = 10, 2, 6, 9;



If you have to initialize c with an array, since it is small, you can just unroll it like



Color c = 10, myRGB[0], myRGB[1], myRGB[2];





share|improve this answer


















  • 1




    I do not think it answers OP's question, looks like they need to initialize from a variable.
    – SergeyA
    23 mins ago






  • 1




    @SergeyA ¯_(ツ)_/¯. The OP hasn't responded for me to know. I figured they used the array object because they didn't know they didn't need it. Hopefully they will respond at some point.
    – NathanOliver
    21 mins ago











  • @SergeyA Added an example of how they could "unroll" the array into the initializer.
    – NathanOliver
    6 mins ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53156499%2finitialize-object-containing-c-style-array-as-member-variable-c%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
10
down vote













Since Color is an aggregate you can use aggregate initialization and put the array initializer directly in the braces like



Color c = 10, 2, 6, 9;



If you have to initialize c with an array, since it is small, you can just unroll it like



Color c = 10, myRGB[0], myRGB[1], myRGB[2];





share|improve this answer


















  • 1




    I do not think it answers OP's question, looks like they need to initialize from a variable.
    – SergeyA
    23 mins ago






  • 1




    @SergeyA ¯_(ツ)_/¯. The OP hasn't responded for me to know. I figured they used the array object because they didn't know they didn't need it. Hopefully they will respond at some point.
    – NathanOliver
    21 mins ago











  • @SergeyA Added an example of how they could "unroll" the array into the initializer.
    – NathanOliver
    6 mins ago














up vote
10
down vote













Since Color is an aggregate you can use aggregate initialization and put the array initializer directly in the braces like



Color c = 10, 2, 6, 9;



If you have to initialize c with an array, since it is small, you can just unroll it like



Color c = 10, myRGB[0], myRGB[1], myRGB[2];





share|improve this answer


















  • 1




    I do not think it answers OP's question, looks like they need to initialize from a variable.
    – SergeyA
    23 mins ago






  • 1




    @SergeyA ¯_(ツ)_/¯. The OP hasn't responded for me to know. I figured they used the array object because they didn't know they didn't need it. Hopefully they will respond at some point.
    – NathanOliver
    21 mins ago











  • @SergeyA Added an example of how they could "unroll" the array into the initializer.
    – NathanOliver
    6 mins ago












up vote
10
down vote










up vote
10
down vote









Since Color is an aggregate you can use aggregate initialization and put the array initializer directly in the braces like



Color c = 10, 2, 6, 9;



If you have to initialize c with an array, since it is small, you can just unroll it like



Color c = 10, myRGB[0], myRGB[1], myRGB[2];





share|improve this answer














Since Color is an aggregate you can use aggregate initialization and put the array initializer directly in the braces like



Color c = 10, 2, 6, 9;



If you have to initialize c with an array, since it is small, you can just unroll it like



Color c = 10, myRGB[0], myRGB[1], myRGB[2];






share|improve this answer














share|improve this answer



share|improve this answer








edited 7 mins ago

























answered 1 hour ago









NathanOliver

81k15110169




81k15110169







  • 1




    I do not think it answers OP's question, looks like they need to initialize from a variable.
    – SergeyA
    23 mins ago






  • 1




    @SergeyA ¯_(ツ)_/¯. The OP hasn't responded for me to know. I figured they used the array object because they didn't know they didn't need it. Hopefully they will respond at some point.
    – NathanOliver
    21 mins ago











  • @SergeyA Added an example of how they could "unroll" the array into the initializer.
    – NathanOliver
    6 mins ago












  • 1




    I do not think it answers OP's question, looks like they need to initialize from a variable.
    – SergeyA
    23 mins ago






  • 1




    @SergeyA ¯_(ツ)_/¯. The OP hasn't responded for me to know. I figured they used the array object because they didn't know they didn't need it. Hopefully they will respond at some point.
    – NathanOliver
    21 mins ago











  • @SergeyA Added an example of how they could "unroll" the array into the initializer.
    – NathanOliver
    6 mins ago







1




1




I do not think it answers OP's question, looks like they need to initialize from a variable.
– SergeyA
23 mins ago




I do not think it answers OP's question, looks like they need to initialize from a variable.
– SergeyA
23 mins ago




1




1




@SergeyA ¯_(ツ)_/¯. The OP hasn't responded for me to know. I figured they used the array object because they didn't know they didn't need it. Hopefully they will respond at some point.
– NathanOliver
21 mins ago





@SergeyA ¯_(ツ)_/¯. The OP hasn't responded for me to know. I figured they used the array object because they didn't know they didn't need it. Hopefully they will respond at some point.
– NathanOliver
21 mins ago













@SergeyA Added an example of how they could "unroll" the array into the initializer.
– NathanOliver
6 mins ago




@SergeyA Added an example of how they could "unroll" the array into the initializer.
– NathanOliver
6 mins ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53156499%2finitialize-object-containing-c-style-array-as-member-variable-c%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery