Re: STL map of structures

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Bob,
I believe that Doug had provided concise and helpful answers here. I just
would like to add additional comments regarding one of your concerns, "The
downside (such as it is) of having a map of structures is having
to add a default constructor and a copy constructor to my structure.".

Generally you need not to add a default constructor and a copy constructor
to your structure since complier will do it implicitly (mostly bitwise
copy). You can simply perform a test with the following code:
struct MyStruct
{
int m_i;
int* p_i;
};

int _tmain(int argc, _TCHAR* argv[])
{
MyStruct s;
int n = 100;
s.m_i = 10;
s.p_i = &n;


map<int, MyStruct> myMap;
myMap[0]=s;
printf("%d,%d",myMap[0].m_i,*(myMap[0].p_i));

getchar();
return 0;
}

Whether or not to use a copy constructor depends on situations. When a
class does not allow/need to use bitwise copy semantics, you need to
explicitly write a copy constructor. I would like to recommend a famous
book (written by Stanley B.Lippman) for you "Inside The C++ Object Model"
(http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545).
You can read the chapter "The senmantics of constructors" in which Stanley
listed four typical situations that do not need "Bitwise Copy Semantics".
Generally they are the following:
1) Member class object having copy constructor
2) Base class having copy constructor
3) Class having one or more virtual functions
4) Derived class having one or more virtual base classes

Hope this helps.

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




.



Relevant Pages

  • Re: Initialize an array of classes?
    ... Currently you cannot use member initialization list for a nonstatic array of a class if it does not have a default constructor. ... however you may encounter the warning C4351 and I do not think that it is useful to use such initialization for an array. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Windows Form Designer generated code
    ... In VS.NET 2003, the part of 'Windows Form Designer generated code' contains a constructor and the InitializeComponent method, and these code is located in the form.vb file. ... You could put your initialization code either in the constructor or the Load event handler of the form. ... 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. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Object oriented stuff and browsers related thing
    ... with Netscape 2.0 being the first browser to support ... iff MyConstr() is called as a constructor. ... It refers to `MyConstr' as that would be the ...
    (comp.lang.javascript)
  • Re: XmlHttpRequest and Anonymous Method
    ... var xmlHttpRequest; ... not all user agents support them. ... well occur in that constructor. ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)
  • RE: Serialization and parameterized constructor
    ... parameterized constructor on your web service but there will be no way to ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework)