473,773 Members | 2,334 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
"initialize r 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 2155
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.m icrosoft.comwro te in message
news:XU******** *****@TK2MSFTNG HUB02.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(voi d) : 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.nos pamwrote in message
news:uG******** ********@TK2MSF TNGP04.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.m icrosoft.comwro te in message
news:XU******* ******@TK2MSFTN GHUB02.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(in t a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
};

class Test {
Test::Test(vo id) : 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
professiona l 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
5938
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
15
5325
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 private string myArray;
16
4718
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
3030
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
6252
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
9125
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: struct st_a { int i, j; };
5
1633
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: #define N 5 bool myData; // Don't want to do this:
3
2130
by: abubakarm | last post by:
Hi, If i have the following struct: struct t { const int x;// = 0; char name ; };
19
1929
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 that, for whatever reason, I don't want to call the constructor of 'Derived', but instead I want to build the object by
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.