Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How many instances???

How many instances???

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Renjith Ramachandran
    wrote on last edited by
    #1

    how can i get the number of instances of my clasess? there is one way like we can declare a static member variable and increment that in the constructor, but if you create a derived object, that count also increases in the base class. how can i prevent that ? i need to get the count of parent objects only. any idea? Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

    T S 2 Replies Last reply
    0
    • R Renjith Ramachandran

      how can i get the number of instances of my clasess? there is one way like we can declare a static member variable and increment that in the constructor, but if you create a derived object, that count also increases in the base class. how can i prevent that ? i need to get the count of parent objects only. any idea? Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

      T Offline
      T Offline
      try88
      wrote on last edited by
      #2

      just write two constructors function 路漫漫其修远兮,吾将上下而求索。

      1 Reply Last reply
      0
      • R Renjith Ramachandran

        how can i get the number of instances of my clasess? there is one way like we can declare a static member variable and increment that in the constructor, but if you create a derived object, that count also increases in the base class. how can i prevent that ? i need to get the count of parent objects only. any idea? Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Decrement the counter by one in your derived class's constructor? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        T R 2 Replies Last reply
        0
        • S S Senthil Kumar

          Decrement the counter by one in your derived class's constructor? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          and set the destructor as virtual


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          R 1 Reply Last reply
          0
          • S S Senthil Kumar

            Decrement the counter by one in your derived class's constructor? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            R Offline
            R Offline
            Renjith Ramachandran
            wrote on last edited by
            #5

            hey how it is possible? how can i say other ppls to decrement parent clasess counter in your clasess? i need to maintain my class as an entity, i just need to know the number of instances of my base class Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

            S 1 Reply Last reply
            0
            • T toxcct

              and set the destructor as virtual


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              R Offline
              R Offline
              Renjith Ramachandran
              wrote on last edited by
              #6

              can u write the code for that ??? Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

              T 1 Reply Last reply
              0
              • R Renjith Ramachandran

                hey how it is possible? how can i say other ppls to decrement parent clasess counter in your clasess? i need to maintain my class as an entity, i just need to know the number of instances of my base class Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                class Base
                {
                protected:
                static int counter;

                public:
                Base() { counter++; }
                };

                int Base::counter;

                class Derived : Base
                {
                public:
                Derived()
                {
                counter--;
                }
                };

                Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                1 Reply Last reply
                0
                • R Renjith Ramachandran

                  can u write the code for that ??? Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  class CBase {
                  static int m_iCounter;

                  public:
                  CBase() {
                  m_iCounter++;
                  }

                  virtual ~CBase() {
                      m\_iCounter--;
                  }
                  

                  };

                  class CChild : public CBase {
                  //...
                  };

                  now, you could do this :

                  void foo() { //Counter == 0
                  CChild myChild1; //Counter == 1
                  CChild myChild2; //Counter == 2
                  {
                  CCHild myChild3; //Counter == 3
                  } //Counter == 2
                  myChild1.~CChild(); //Counter == 1
                  } //Counter == 0

                  when the program goes out a scope, the objects destructor is automatocally called. for a CCHild object, the CCHild destructor is firstly call, then its CBase destructor... was it well what you wanted ?


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  S 1 Reply Last reply
                  0
                  • T toxcct

                    class CBase {
                    static int m_iCounter;

                    public:
                    CBase() {
                    m_iCounter++;
                    }

                    virtual ~CBase() {
                        m\_iCounter--;
                    }
                    

                    };

                    class CChild : public CBase {
                    //...
                    };

                    now, you could do this :

                    void foo() { //Counter == 0
                    CChild myChild1; //Counter == 1
                    CChild myChild2; //Counter == 2
                    {
                    CCHild myChild3; //Counter == 3
                    } //Counter == 2
                    myChild1.~CChild(); //Counter == 1
                    } //Counter == 0

                    when the program goes out a scope, the objects destructor is automatocally called. for a CCHild object, the CCHild destructor is firstly call, then its CBase destructor... was it well what you wanted ?


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc]

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    Writing a virtual destructor for a base class is generally a good idea, but is not absolutely necessary for this application. That apart, my interpretation of the OP's question was that he wanted to count only explicitly created instances of CBase, that's why I suggested decrementing it in derived class constructors. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups