-
get all objects for a class
I need to loop through all of the objects I have created for a class in order to set and/or examine the objects' variables.
I have a class:
__gc class Human
{
public:
int height;
int weight;
String *name;
};
For which I have created several objects (people)
Is there a 'for_all_objects_of(class)' type of command I can use or do I need to have a list of all objects created and run through the list with a 'for' loop?
I have tried to build a list of objects:
std::list <Human> listofhumans;
but the Visual C++ compiler says this is 'an illegal use of manged type'.
I was hoping I don't need to use a list at all, can anyone help?
-
There's no such feature in standard C++, although you can easily accomplish it by using a design patterns such as Visitor. You can also create a vector (list is a bad idea anyway because it doesn't support the subscript operator). However, STL and managed code don't go together so the real question is: do you really need to use manged C++?
Danny Kalev
-
What's the difference between Managed C++ and the other?
Also, what is the design pattern 'Visitor'?
Could you show me how to initialize a vector of objects?
Thanks for your help
-
Originally posted by dbrown
What's the difference between Managed C++ and the other?
Also, what is the design pattern 'Visitor'?
Could you show me how to initialize a vector of objects?
Thanks for your help
Managed C++ is a non-standard, Microsoft proprietary extension to ANSI/ISO C++. One of the advantages of using MC++ is garbage collection. However, MC++ has many drawbacks. It doesn't support templates (that is why your code failed to compile) and many other standard C++ features. In short, unless you're absolutely positive that you need to use MC++, avoid it and stick to vanilla C++ (e.g., a Console Application).
You can learn how to use vector here: http://www.devx.com/DevX/LegacyLink/9396
Design Patterns are recipes for solving common programming tasks. You can find more information about Patterns here: http://www.dofactory.com/Patterns/Patterns.aspx#list
Danny Kalev
-
Thanks Danny,
regrettable I have to stcik with the managed C++. Do you know the best direction to accomplish my list/set/vector/array of objects?
-
There are several possibilities: either use the Microsoft proprietary collection classes that are available for MC++ (I'm not familar with these but there shoudl be such classes), or write a custome collection class that works like vector. One thing's certain: you can't use STL with MC++, at least not yet.
Danny Kalev
-
I appreciate all of your advise, Danny!
I worked out that I can use an array of struct but not an array of class so I'm all set to go.
Thanks again,
Dave
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks