How many instances???
-
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.
-
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.
-
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.
Decrement the counter by one in your derived class's constructor? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Decrement the counter by one in your derived class's constructor? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Decrement the counter by one in your derived class's constructor? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
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.
-
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.
-
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.
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
-
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.
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 == 0when 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] -
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 == 0when 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]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