Numbered class

Discussion in 'C++' started by jalina, Apr 21, 2006.

  1. jalina

    jalina Guest

    Hello,

    I want to have classes that all have a unique identifier. It is easy for
    a single class:

    class A {
    static int index;
    int my_index;
    A(): my_index(index++) { }
    };


    But if you have something like:

    class B: public A {
    B() //....
    }

    class C: public A {
    C() //....
    }

    the "index" is shared among A, B and C's. How can I do to have different
    index for each distinct class without having to redefine each time a new
    index ?

    Thank you from Jalina
     
    jalina, Apr 21, 2006
    #1
    1. Advertising

  2. jalina

    Ian Collins Guest

    jalina wrote:
    > Hello,
    >
    > I want to have classes that all have a unique identifier. It is easy for
    > a single class:
    >
    > class A {
    > static int index;
    > int my_index;
    > A(): my_index(index++) { }
    > };
    >
    >
    > But if you have something like:
    >
    > class B: public A {
    > B() //....
    > }
    >
    > class C: public A {
    > C() //....
    > }
    >
    > the "index" is shared among A, B and C's. How can I do to have different
    > index for each distinct class without having to redefine each time a new
    > index ?
    >

    You can't.

    You have to have one per derived class.

    --
    Ian Collins.
     
    Ian Collins, Apr 21, 2006
    #2
    1. Advertising

  3. jalina

    ziman137 Guest

    Maybe you can try this way:

    1) build another Index class:

    class Index {
    static int index = 0;
    int my_index;
    Index(): my_index( index++ ) {};
    int val() { return my_index; };
    // ...
    }

    2) wrap Index objects in your derived classes:

    class B: public A {
    Index indObj;
    int getIndex() {...}; // return indObj.val();
    B() //....
    }

    class C: public A {
    Index indObj;
    int getIndex() {...}; // return indObj.val();
    C() //....
    }

    3) for each object "B b()", or "C c()" ... associated index by

    b.getIndex(), ...

    Just a quick idea, didn't try it - supposedly to have lots of glitches,
    ;) you may work it out. My quick thought is about two guidelines:

    1. never try and prevent manually constructing Index objects, so it is
    better hidden and wrapped as private in another namespace, while
    accessible by B, C, ....
    2. depending on the context of your application, you may let index
    monotonically gorw, or not.

    Another idea is to use smart-pointer, reference pointer counting
    pattern, which may need a bit more detailed work-out. Anyway, these're
    just some quick brainstorming.

    Gary
    -: peace

    jalina wrote:
    > Hello,
    >
    > I want to have classes that all have a unique identifier. It is easy for
    > a single class:
    >
    > class A {
    > static int index;
    > int my_index;
    > A(): my_index(index++) { }
    > };
    >
    >
    > But if you have something like:
    >
    > class B: public A {
    > B() //....
    > }
    >
    > class C: public A {
    > C() //....
    > }
    >
    > the "index" is shared among A, B and C's. How can I do to have different
    > index for each distinct class without having to redefine each time a new
    > index ?
    >
    > Thank you from Jalina
     
    ziman137, Apr 21, 2006
    #3
  4. jalina

    Fei Liu Guest

    jalina wrote:
    > Hello,
    >
    > I want to have classes that all have a unique identifier. It is easy for
    > a single class:
    >
    > class A {
    > static int index;
    > int my_index;
    > A(): my_index(index++) { }

    Add a public interface here:
    int getIndex() const { return index; }
    > };
    >
    >
    > But if you have something like:
    >
    > class B: public A {
    > B() //....
    > }
    >
    > class C: public A {
    > C() //....
    > }
    >
    > the "index" is shared among A, B and C's. How can I do to have different
    > index for each distinct class without having to redefine each time a new
    > index ?


    All your class A, B, C objects are already guranteed to have different
    index, all that needs to be done is a way to expose its value.
    >
    > Thank you from Jalina
     
    Fei Liu, Apr 21, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rado
    Replies:
    13
    Views:
    730
    Mark Parnell
    May 11, 2004
  2. donny
    Replies:
    2
    Views:
    9,000
    Toby Inkster
    Aug 8, 2006
  3. Replies:
    24
    Views:
    704
    CBFalconer
    Jun 9, 2006
  4. Replies:
    0
    Views:
    231
  5. Dr. Phillip M. Feldman

    recursive algorithm for balls in numbered boxes

    Dr. Phillip M. Feldman, Sep 11, 2011, in forum: Python
    Replies:
    2
    Views:
    496
    Dr. Phillip M. Feldman
    Sep 12, 2011
Loading...

Share This Page