Counting objects of the same class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Istrate

    Counting objects of the same class

    Hello,

    I have a class A and I want to know at every moment what is the number
    of objects of type A. I know that a good solution for this is to add a
    static variable (member of class A) and increment it in constructor
    and decrement it in destructor. But what if I inherit class A in more
    classes (B, C, etc)? Every constructor of a child class will call the
    constructor of A, so the counting will fail. What solution do you
    recommend me?

    Thank you,
    Stefan Istrate
  • AnonMail2005@gmail.com

    #2
    Re: Counting objects of the same class

    On Jul 17, 10:48 am, Stefan Istrate <stefan.istr... @gmail.comwrote :
    Hello,
    >
    I have a class A and I want to know at every moment what is the number
    of objects of type A. I know that a good solution for this is to add a
    static variable (member of class A) and increment it in constructor
    and decrement it in destructor. But what if I inherit class A in more
    classes (B, C, etc)? Every constructor of a child class will call the
    constructor of A, so the counting will fail. What solution do you
    recommend me?
    >
    Thank you,
    Stefan Istrate
    You can have more than one constructor (or an argument to a
    constructor)
    that sets a boolean inside the class telling it whether to count a
    particular
    instance. Instances of type A can be constructed with the arg set to
    true
    and other derived classes can set the arg to false.

    Of course, you should put the creation of the object behind a function
    (i.e. factory) so your client code doesn't have to know about this.

    HTH

    Comment

    • Markus Moll

      #3
      Re: Counting objects of the same class

      Hi

      AnonMail2005@gm ail.com wrote:
      You can have more than one constructor (or an argument to a
      constructor)
      that sets a boolean inside the class telling it whether to count a
      particular
      instance. Instances of type A can be constructed with the arg set to
      true
      and other derived classes can set the arg to false.
      >
      Of course, you should put the creation of the object behind a function
      (i.e. factory) so your client code doesn't have to know about this.
      virtual base classes can also help:

      struct A_counter
      {
      static int count;
      bool count_me;
      A_counter(bool count_me = false)
      : count_me(count_ me)
      {
      if(count_me) ++count;
      }

      ~A_counter()
      {
      if(count_me) --count;
      }
      };

      struct A : protected virtual A_counter
      {
      A() : A_counter(true) {}
      };

      struct D : A
      {
      };

      Markus


      Comment

      • Daniel T.

        #4
        Re: Counting objects of the same class

        Stefan Istrate <stefan.istrate @gmail.comwrote :
        I have a class A and I want to know at every moment what is the number
        of objects of type A. I know that a good solution for this is to add a
        static variable (member of class A) and increment it in constructor
        and decrement it in destructor. But what if I inherit class A in more
        classes (B, C, etc)? Every constructor of a child class will call the
        constructor of A, so the counting will fail.
        Why do you consider that a failure, objects of child classes are also
        type A objects...
        What solution do you recommend me?
        Others have suggested two constructors, one for children and one for
        everybody else, this assumes that children are going to use the correct
        constructor though and requires that you go through all existing
        children and change them.

        In the future, I recommend you make all base classes abstract. I.E., the
        only type A objects that should be getting created are those from child
        classes of A.

        Comment

        • Markus Moll

          #5
          Re: Counting objects of the same class

          Hi

          Daniel T. wrote:
          Others have suggested two constructors, one for children and one for
          everybody else, this assumes that children are going to use the correct
          constructor though and requires that you go through all existing
          children and change them.
          Not necessarily, as you can rely on default constructors and access
          specifiers to make sure that A is the only class that could possibly call
          the constructor that invokes counting, while all other classes would do the
          right thing without any modification.
          In the future, I recommend you make all base classes abstract. I.E., the
          only type A objects that should be getting created are those from child
          classes of A.
          While it's true that you often want interfaces, I cannot see any
          justification for that generalization.

          Markus


          Comment

          • Daniel T.

            #6
            Re: Counting objects of the same class

            Markus Moll <markus.moll@es at.kuleuven.ac. bewrote:
            Daniel T. wrote:
            >
            In the future, I recommend you make all base classes abstract. I.E., the
            only type A objects that should be getting created are those from child
            classes of A.
            >
            While it's true that you often want interfaces, I cannot see any
            justification for that generalization.
            If you have a class "A" and you make a sub-class of it "B" you also
            (whether you want to or not) implicitly make another "sub-class" (all
            A's that are not also B's.) In other words, the generalization
            implicitly exists, whether you can see the justification for it or not.

            This very problem is a perfect example, the OP wants to count all A's
            that are not also B's (or C's or whatever.) If he had a class to
            explicitly represent this concept, the solution would have been so
            straight forward, he probably wouldn't have had to ask how.

            Comment

            Working...