473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

generics method?

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

Aug 18 '07 #1
2 1145

"vcquestions" <vc*********@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
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??
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 Twhere T : IDisposable
void FreeAll(IEnumerable<T>^ theList)
{
for each (T^ member in theList)
delete member;
}
>
Thanks in advance!
vcq

Aug 20 '07 #2
On Aug 20, 6:49 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"vcquestions" <vcquesti...@gmail.comwrote in message

news:11**********************@d55g2000hsg.googlegr oups.com...


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??

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 Twhere T : IDisposable
void FreeAll(IEnumerable<T>^ theList)
{
for each (T^ member in theList)
delete member;

}
Thanks in advance!
vcq- Hide quoted text -

- Show quoted text -- Hide quoted text -

- 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.

Aug 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
4
by: Random | last post by:
I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.