473,511 Members | 17,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to initialize a member struct

Hi all,

If I have a class that includes an instance of a struct as a member, how do
I initialize that struct? I can't find a syntax for the constructor
"initializer list" that works.

For example, suppose MyStruct has 3 int members. I've tried something like
this:

class Test {
private:
MyStruct X
}

// Constructor
Test::Test(void) : X(1,2,3) // Try initializing all 3 struct members...
Nov 29 '07 #1
3 2139
Hi Bob,

Using initializer list for another structure member means calling that
structure's constructor. So that structure must has a constructor matching
the signature with the initializer list calling, like this:
struct MyStruct
{
int m_a;
int m_b;
int m_c;

MyStruct(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
};

class Test {
Test::Test(void) : X(1,2,3)
{
}
private:
MyStruct X;
};
Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 29 '07 #2
If I create a MyStruct in "normal" code, I can just write:
>
MyStruct x = {1, 2, 3};

But I apparently can't include the initializer list (the "{1, 2, 3}") in a
class definition header file where I declare a member variable of type
MyStruct. This leaves me with trying to come up with a way to initialize
the struct in the class constructor. The ugly way to do this is to
explicitly initialize each member of the structure in the constructor
code. But I'm looking for a way to use an initializer list to initialize
the entire struct all at once.
With a helper function:

static MyStruct init_my_struct(int a, int b, int c)
{
const MyStruct x = { a, b, c };
return x;
}

MyConstructor() : member(init_my_struct(1, 2, 3)) {}

Sigh... I told you it was complicated...
>
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:XU*************@TK2MSFTNGHUB02.phx.gbl...
>Hi Bob,

Using initializer list for another structure member means calling that
structure's constructor. So that structure must has a constructor
matching
the signature with the initializer list calling, like this:
struct MyStruct
{
int m_a;
int m_b;
int m_c;

MyStruct(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
};

class Test {
Test::Test(void) : X(1,2,3)
{
}
private:
MyStruct X;
};
Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
================================================= =
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
================================================= =
This posting is provided "AS IS" with no warranties, and confers no
rights.



Nov 29 '07 #3
Thanks Ben. I guess that's about as clean a solution as I'm likely to get.

- Bob

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:uG****************@TK2MSFTNGP04.phx.gbl...
>If I create a MyStruct in "normal" code, I can just write:

MyStruct x = {1, 2, 3};

But I apparently can't include the initializer list (the "{1, 2, 3}") in
a class definition header file where I declare a member variable of type
MyStruct. This leaves me with trying to come up with a way to initialize
the struct in the class constructor. The ugly way to do this is to
explicitly initialize each member of the structure in the constructor
code. But I'm looking for a way to use an initializer list to initialize
the entire struct all at once.

With a helper function:

static MyStruct init_my_struct(int a, int b, int c)
{
const MyStruct x = { a, b, c };
return x;
}

MyConstructor() : member(init_my_struct(1, 2, 3)) {}

>Sigh... I told you it was complicated...
>>
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:XU*************@TK2MSFTNGHUB02.phx.gbl...
>>Hi Bob,

Using initializer list for another structure member means calling that
structure's constructor. So that structure must has a constructor
matching
the signature with the initializer list calling, like this:
struct MyStruct
{
int m_a;
int m_b;
int m_c;

MyStruct(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
};

class Test {
Test::Test(void) : X(1,2,3)
{
}
private:
MyStruct X;
};
Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
================================================ ==
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach
the
most efficient resolution. The offering is not appropriate for
situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
best
handled working with a dedicated Microsoft Support Engineer by
contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
================================================ ==
This posting is provided "AS IS" with no warranties, and confers no
rights.




Nov 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5923
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
15
5301
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
16
4690
by: Chad | last post by:
How do I set the value of a = 3 in the following lines of code? #include <stdio.h> struct letter{ char a; }; struct add { struct letter addit;
10
2994
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
6201
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
18
9086
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
5
1616
by: Bob Altman | last post by:
Hi all, Here's a really basic C++ syntax question: How do I initialize an array, whose size is a literal, to all zeros in its initializer without coding a loop to do it? For example: ...
3
2115
by: abubakarm | last post by:
Hi, If i have the following struct: struct t { const int x;// = 0; char name ; };
19
1904
by: Juha Nieminen | last post by:
Assume I have a class Base, and then this: struct Derived: public Base { AnotherClass member; }; Also assume that I'm allocating an object of type 'Derived' with a custom allocator and...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.