Path: utzoo!attcan!uunet!mcsun!ukc!edcastle!ecsv38 From: ecsv38@castle.ed.ac.uk (S Manoharan) Newsgroups: comp.lang.c++ Subject: Re: Design Questions : Help !!! Keywords: Object Identity Message-ID: <4925@castle.ed.ac.uk> Date: 27 Jun 90 11:11:06 GMT References: <23694@uflorida.cis.ufl.EDU> Reply-To: ecsv38@castle.ed.ac.uk (S Manoharan) Organization: Dept of Computer Science, University of Edinburgh Lines: 48 In article <23694@uflorida.cis.ufl.EDU> sml@beach.cis.ufl.edu (Shein-Fong Law) writes: > > I am trying to implement an object idendity generator for my database >project. I want to assign an instance i.d (i.i.d) for each object created. The >format of i.i.d is a concatenation of class i.d (c.i.d) and object i.d (o.i.d). >All the objects belonging to the same class will have the same c.i.d but >different (unique) o.i.d's. Internally, the identifiers can be just unsigned >integers. > > Also, I want the creation of such identifiers to be known by the system only >(in this case, me). Of course, the design should be object-oriented. > One possible way could be: class Root { private: char *cname; int oid; protected: void name(char *const s) { cname = s; } public: Root(char * = 0); int id() { return oid; } char *name() const { return cname; } int class_id() { return (int)cname; } }; Root::Root(char *s) // should not be inline { static int ob = 0; oid = ob++; cname = s ? s : "Root"; } class Leaf : public Root { public: Leaf() : ("Leaf") { /* */ } }; All objects, whether they belong to the same class or not, will have different ids. Class names are to be given whenever you derive a class from Root. (This will also help in debugging!) Address of class names can serve as class ids. Manoharan.