how would you count objects of same name in OOD

I can provide code if needed.

I have a linked list. With and object type of another class.
It works.


How would you count objects of the same name generally? what is your approach?
e,g'

object is foo.
if I add another foo object to my list with the name of foo?

how would you count an new object? would you overload ++ -- ? posfix prefex?
or would you add a data member to the object called count and then increment it algorithmically?
Are you talking about objects of the same type or objects with the same name? Those are 2 very different things so you have to be more precise.

If you are referring to type, then simply incrementing a counter variable will do the trick. Unless you are dealing with polymorphism, then you might just have to overload a count method for each object type.

If you mean't name, then use std::map<std::string, int>
http://www.cplusplus.com/reference/map/map/map/
Or unordered_map
http://www.cplusplus.com/reference/unordered_map/unordered_map/unordered_map/
Last edited on
I am asking about objects of the same type and same name.
(sorry drinking a bit)

the invariant logic I am attempting to create is.

add object -
if name of object exists.
increment object count. and do not create a new object.
Topic archived. No new replies allowed.