Re: Initialize an array of classes?



Hi Bob,
Currently you cannot use member initialization list for a nonstatic array of a class if it does not have a default constructor. When you try to initialize the array, you will encounter the compiler
error C2536. You may refer to:
Compiler Error C2536
http://msdn.microsoft.com/en-us/library/9f53ks1w.aspx

If the class has a default constructor, you can explicitly initialize it like the following code:
class CInner {
public:
// Constructor
CInner(int x) : Value(x) {}
CInner(){}

// Member variable
int Value;
};

class COuter {
public:
CInner InnerValue;
CInner m_Inners[2];

// Constructor
COuter() :
InnerValue(2),m_Inners()
{}
};

The code can be compiled, however you may encounter the warning C4351 and I do not think that it is useful to use such initialization for an array.
You can refer to this article for further inforamtion:
Compiler Warning (level 1) C4351
http://msdn.microsoft.com/en-us/library/1ywe7hcy.aspx

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

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/subscriptions/support/default.aspx.
============================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================


.



Relevant Pages

  • Re: valarray problem
    ... > P.J. Plauger wrote: ... >> initialization for the array elements. ... > I didn't use the default constructor - my test code looked like this: ... > available (and otherwise no initialization be done). ...
    (microsoft.public.vc.language)
  • Re: Interesting VS 2003 designer behavior
    ... I see that your collection item class has 2 overloading constructor ... But the collection class's AddRange is to add the class item array into the ... I am not sure if VS 2005 supports this way of implementing AddRange method, ... Microsoft Online Partner Support ...
    (microsoft.public.dotnet.framework.windowsforms.designtime)
  • Re: Windows Form Designer generated code
    ... Designer generated code' does not contain a constructor. ... It depends on what kind of the initialization code is. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Rationale for C++/CLI Value Types not having a default constructor
    ... Infrastructure) does support value types with parameterless constructors, ... The most obvious reason why parameterless ctros are not supported is fast array initialization. ... If the programmer wants to provide a user-defined default constructor for a type, then filling an array should construct each value of that type with that default constructor. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: valarray problem
    ... extra code to *ensure* that the default constructor initializes ... The array created by this constructor has a length equal to the value of the argument. ... The elements of the array are constructed using the default constructor for the instantiating type T. ... That can be interpreted as meaning that elements should be default-initialized or that they should be value-initialized, or that default constructors should be used if available (and otherwise no initialization be done). ...
    (microsoft.public.vc.language)