Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2008

Tip: Looking for answers? Try searching our database.

'New' in initialization list.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rwf_20 - 25 Apr 2008 22:08 GMT
In standard C++, it is recommended to avoid unmanaged resource
acquisition in initialization lists, such as:

class C {
public:
 C(const int i) : m_i(new int(i)) { }
private:
 int m_i;
};

I'm assuming this holds for code built via /clr as well.  But, do the
'Smart' features of .NET ref types ('^') make this acceptable with
managed types?  E.g.

ref class C {
public:
 C() : m_obj(gcnew ManagedObject) { }
private:
 ManagedObject^ m_obj;
};

Is this safe in VC 8?

Ryan
Carl Daniel [VC++ MVP] - 26 Apr 2008 04:10 GMT
> In standard C++, it is recommended to avoid unmanaged resource
> acquisition in initialization lists, such as:
[quoted text clipped - 18 lines]
>
> Is this safe in VC 8?

Perfectly safe.  GC will (eventually) recover the object if the constructor
throws an exception, which is the concern with using new in a
ctor-initializer in native code.

-cd
Ben Voigt [C++ MVP] - 26 Apr 2008 22:03 GMT
> In standard C++, it is recommended to avoid unmanaged resource
> acquisition in initialization lists, such as:
[quoted text clipped - 18 lines]
>
> Is this safe in VC 8?

Will m_obj be reassigned to point to a different object?  If not, then use
the stack semantics notation so that Dispose is called on m_obj if the
constructor throws.  If it *can* be reassigned and is IDisposable, then does
this class acquire ownership of any other object ever held in m_obj?  You
may still want to use RAII to guarantee Dispose is called appropriately (in
C++, you use the delete keyword to call IDisposable::Dispose).

> Ryan

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.