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++ / August 2007

Tip: Looking for answers? Try searching our database.

generics method?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vcquestions - 18 Aug 2007 05:25 GMT
What's the best approch to having a common method that takes a
collection ( List<> ), goes through its elements and deletes them.

    public ref struct Struct1
    {
        public:
            Struct1( );
            ~Struct1( );
    };

    public ref struct Struct2
    {
        public:
            Struct2( );
            ~Struct2( );
    };

    typedef List<Struct1^>^  StructuresOne;
    typedef List<Struct12>^  StructuresTwo;

    ??method that would accept StructuresTwo or StructuresOne and call
destructor on each object??

Thanks in advance!
vcq
Ben Voigt [C++ MVP] - 20 Aug 2007 14:49 GMT
> What's the best approch to having a common method that takes a
> collection ( List<> ), goes through its elements and deletes them.
[quoted text clipped - 18 lines]
> ??method that would accept StructuresTwo or StructuresOne and call
> destructor on each object??

No generics needed:

void FreeAll(IEnumerable theList)
{
   for each (System::Object^ member in theList)
       delete dynamic_cast<IDisposable^>(member);
}

However, using generic would yield a speed improvement for "value
class"/"value struct" members:

generic <typename T> where T : IDisposable
void FreeAll(IEnumerable<T>^ theList)
{
   for each (T^ member in theList)
       delete member;
}

> Thanks in advance!
> vcq
vcquestions - 20 Aug 2007 17:51 GMT
> > What's the best approch to having a common method that takes a
> > collection ( List<> ), goes through its elements and deletes them.
[quoted text clipped - 45 lines]
>
> - Show quoted text -

Ben, thank you for a thorough answer.  Why do we want to cast to the
IDisposable ( in the first approach ) instead of applying delete to
the Object^.  I thought delete on the object was a correct thing to do
and if our item is based on the object than the virtual destructor
will be called.  Are we not hitting extra overhead with dynamic_cast
here?

Thanks again.
Ben Voigt [C++ MVP] - 20 Aug 2007 18:26 GMT
>> > What's the best approch to having a common method that takes a
>> > collection ( List<> ), goes through its elements and deletes them.
[quoted text clipped - 52 lines]
> will be called.  Are we not hitting extra overhead with dynamic_cast
> here?

I'm not sure if delete on an Object will add a runtime check whether it is
IDisposable.  In order to call a virtual member function, you normally have
to downcast to at least the type where that member function is introduced,
and "Dispose" is introduced by IDisposable.

The runtime check is necessary when using Object, because not all objects
implement Disposable, so there's no overhead.

That's one of the reasons why the generic method could be faster, because
now there is a constraint and the compiler knows in advance that T
implements Dispose.  However a cast to interface probably isn't any faster
than dynamic_cast anyway (cast to base class is very fast).  The JIT only
instantiates a generic once for reference types, the same implementation is
shared for all different T unless T is a value struct.  So it's not possible
for the compiler to determine the correct offset into the v-table at JIT
time, a runtime cast will be used.
vcquestions - 20 Aug 2007 19:12 GMT
> >> "vcquestions" <vcquesti...@gmail.com> wrote in message
>
[quoted text clipped - 75 lines]
>
> - Show quoted text -

crystal clear! ;)  thank you!

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.