Re: Managing multiple instances



Ben,

I wanted to start improving this design by suppling
the identifier rather than leaving it up to the data
structure to assign the identifier.

Which sounds like a good idea. That would bring together three
responsibilities which sound like a consistent package:
- creating instances of a class (say A)
- assigning identifiers to instances
- remembering which identifiers point to which instances

Possible but then how does the identifier get shared with the client
code so it can look up the object in the data structure?

Going back to the example we were discussing, you'd need to set up some
collaboration between B and C. Callbacks would work:

....instance of C is created: C.1 ...
... C.1 creates an instance of B (B.1), passing a pointer to itself
... B.1 asks its "parent" C for an "A identifier" (yielding, say, 1)
... B.1 creates an instance of A (A.1) and assigns it identifier 1
... C.1 creates an instance of B (B.2), passing a pointer to itself
... B.2 asks its "parent" C for an "A identifier" (yielding, say, 2)
... B.2 creates an instance of A (A.2) and assigns it identifier 2

Get your hands on a copy of Michael Feather's /Working Effectively With
Legacy Code/... Seems to have been written just for you.

I own it, can you suggest a particular section?

Well, this "data structure" sounds like it's effectively a collection of
globals, so I'd have a second look at the sections relevant to
mitigating the ill effects of global references.

Laurent
.



Relevant Pages

  • Re: "Mastering C Pointers"....
    ... the name of an anonymous object. ... or a pointer to an anonymous and typeless object. ... The value of an object depends on the bit pattern in the object ... An object may be accessed safely by any type of identifier ...
    (comp.lang.c)
  • Re: "Mastering C Pointers"....
    ... > Alan Connor wrote: ... > does not apply to register objects. ... > or a pointer to an anonymous and typeless object. ... > and on the type of identifier used to access the object. ...
    (comp.lang.c)
  • Re: int (*daytab)[13] vs int *daytab[13]
    ... > is a pointer to an array of 13 integers. ... everything enclosed within the parens starting with the most ... we always start with an identifier first. ... We see the following: and int ...
    (comp.lang.c)
  • Re: int* const ptr
    ... > to use the identifier 'ptr' in this context. ... the name of the function is func and the name of the pointer is ... about how the language/computer works than actually modelling/solving a ...
    (alt.comp.lang.learn.c-cpp)
  • Re: operation on pointer
    ... it operates on the pointer ... If the identifier is enclosed inside parantheses, the same two rules, above, ... Here ptr is the identifier, and is enclosed in the parantheses. ... and on it's right is ++ (increments ptr). ...
    (comp.lang.c)