Initialize object containing c-style array as member variable (c++)
Clash 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).
c++ arrays constructor
add a comment |Â
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).
c++ arrays constructor
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 theColor
structure.
– Some programmer dude
1 hour ago
add a comment |Â
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).
c++ arrays constructor
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
c++ arrays constructor
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 theColor
structure.
– Some programmer dude
1 hour ago
add a comment |Â
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 theColor
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
add a comment |Â
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];
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
add a comment |Â
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];
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
add a comment |Â
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];
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
add a comment |Â
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];
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];
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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