views:

128

answers:

3

I have a piece of code like this

class Base
{
public:
Base(bool _active)
{
 active = _active;
}
void Configure();
void Set Active(bool _active);
private: 
bool active;
};
class Derived1 : public Base
{
public:
Derived1(bool active):Base(active){}

};

similarly Derived 2 and Derived 3

Now if i call derived1Object.Configure, i need to check how many of the derived1Obj, derived2Obj,derived3Obj is active. Should i add this in the "Base" class like a function say, GetNumberOfActive()?

And If the implementation is like this:

class Imp
{
public:
 void Configure()
 {
  //Code instantiating a particular Derived1/2/3 Object 
  int GetNumberOfActiveDerivedObj();
  baseRef.Configure(int numberOfActiveDerivedClasses);
 }
prive:
Derived1 dObj1(true);
Derived2 dObj2(false);
Derived3 dObj3(true);
};

should i calculate the numberOfActive Derived Objects in Imp Class?

THanks

+3  A: 

One simple possibility:

NumberOfActive should be static in Base. It should get incremented every time the object gets created and active is true.

aJ
This only keep track of objects created with active=true but doesn't do anything if active is changed later. Also you can't differentiate the inherited class objects
DaClown
A: 

in Configure() method you may access to a factory of Derived* objects and get the count of active obcects

Arseny
+2  A: 

You could use CRTP in conjunction with a static counter variable: Wikipedia Link

edit: some code

#include <iostream>

template <typename T> struct counter {
    counter() { ++objects_alive; }
    virtual ~counter() { --objects_alive; }
    static int objects_alive;
};
template <typename T> int counter<T>::objects_alive( 0 );


class Base {
public:
    void Configure();
    //more stuff
};

class Derived1 : public Base, counter<Derived1> {
public:
    void Configure() {
        std::cout << "num Derived1 objects: "<< counter<Derived1>::objects_alive << std::endl;
    }
};

class Derived2 : public Base, counter<Derived2> {
public:
    void Configure() {
        std::cout << "num Derived2 objects: " << counter<Derived2>::objects_alive << std::endl;
    }
};

int main (int argc, char* argv[]) {

    Derived1 d10;
    d10.Configure();
    {
        Derived1 d11;
        d11.Configure();
        Derived2 d20;
        d20.Configure();

    }
    Derived1 d12;
    d12.Configure();
    Derived2 d21;
    d21.Configure();


    return 0;
}

Output:

$ g++-4 -pedantic crtp1.cpp -o crtp1 && ./crtp1
num Derived1 objects: 1
num Derived1 objects: 2
num Derived2 objects: 1
num Derived1 objects: 2
num Derived2 objects: 1
DaClown
The idea seems to fit, however I'll certainly not upvote a oneliner. Could you add a code example ?
Matthieu M.
Your wish is my command
DaClown
I don't see a need for a virtual destructor for counter here, a protected destructor should be sufficient.
Georg Fritzsche
simple rule of thumb: if a base class is meant to be inherited from make its destructor virtual
DaClown
But that rule has a background - do that for classes whose lifetime you expect to be managed through pointers to the base and i can't see that being useful with `counter`. A protected destructor prevents people from doing that by mistake.
Georg Fritzsche
tis a bit o' over kill, no?A simple static int adjusted in the base constructor and destructor would do the job. OF Course none of this would be thread safe.
baash05
This counts the number of objects that are alive. But i want to find the number of objects that has active member varaible set as true. active is an ambiguous word. Say if Enable is a bool data member, i need find the objects that are enabled or not. THanks for the help
@baash05 a simple static int won't do because you can't differentiate the inherited objects, just the number of created bases
DaClown
@vivekeviv okay then, how about a factory that keeps track of the objects it created by reference counting and provides a function that returns what ever you want to know about the created objects?
DaClown