implementation of a c++ counter for inherited classes that is visible in default constructor

so i've tried multiple ways of creating a counter for my class that's passing the number in the constructor

i want it to output something like this:

input the amount of base class objects: 2
The A# 1:
input SomeVar_one: 3
input SomeVar_two: 2
The A# 2:
input SomeVar_one: 2
input SomeVar_two: 3
input the amount of derived class objects: 2
The A# 1:
input SomeVar_one: 23
input SomeVar_two: 32
The A# 2:
input SomeVar_one: 12
input SomeVar_two: 42
total # of base objects is: 4
total # of derived objects is: 4

where i do get this:

input the amount of base class objects: 2
The A# 1:
input SomeVar_one: 3
input SomeVar_two: 2
The A# 2:
input SomeVar_one: 2
input SomeVar_two: 3
input the amount of derived class objects: 2
The A# 3:
input SomeVar_one: 23
input SomeVar_two: 32
The A# 4:
input SomeVar_one: 12
input SomeVar_two: 42
total # of base objects is: 4
total # of derived objects is: 4

notice the difference between the user prompts for inputting the values that is called from the constructor that is called from the new operator

ofc there is a need to give them their separate names and this is a question that i'd like to adress as well. anything you might suggest is acceptable.

the main goal is to instantiate objects right at the moment when the new operator is called without getting in the for loop at which i ofc can call "The (A/B) # (i)"

here is a code that i've used: it uses the "curiously recurring template pattern" (btw this name is a killer!)

//sample
#include<iostream>
template <typename T>
class counter
{
public:
    counter(bool do_count = true) : counted(do_count)
    {
        if (counted) get_count()++;
    }
    ~counter()
    {
        if (counted) get_count()--;
    }
    static unsigned long GetInstancesCount()
    {
        return get_count();
    }

private:
    bool counted;
    static unsigned long& get_count()
    {
        static unsigned long count = 0;
        return count;
    }
};


class base:public counter<base>
{
protected:
    int m_SomeVar_one = 0, m_SomeVar_two = 0;
    unsigned long ObjectNumber = 0;
public: 
    base(bool count=true):counter<base>(count),ObjectNumber(counter<base>::GetInstancesCount())
    {
        std::cout << "The A# " << ObjectNumber <<": " << std::endl;
        std::cout << "input SomeVar_one: ";
        std::cin >> m_SomeVar_one;
        std::cout << "input SomeVar_two: ";
        std::cin >> m_SomeVar_two;
    }
    virtual int get_count()
    {
        return counter<base>::GetInstancesCount();
    }
};

class derived :public base, public counter<derived>
{
protected:
    int m_SomeVar_three = 0;

public:
    derived(bool count = true) :base(false), counter<derived>(count)
    {
        std::cout << "input SomeVar_three: ";
        std::cin >> m_SomeVar_three;
    }
    virtual int get_count() override
    {
        return counter<derived>::GetInstancesCount();
    }
};

int main()
{
    std::cout << "input the amount of base class objects: ";
    int amountOfBaseObjects;
    std::cin >> amountOfBaseObjects;
    base* BaseArray = new base[amountOfBaseObjects];


    std::cout << "input the amount of derived class objects: ";
    int amountOfDerivedObjects;
    std::cin >> amountOfDerivedObjects;
    base* DerivedArray = new base[amountOfDerivedObjects];

    std::cout <<"total # of base objects is: "<< BaseArray->get_count() << std::endl;
    std::cout << "total # of derived objects is: " << DerivedArray->get_count() << std::endl;
}


Read more here: https://stackoverflow.com/questions/64396538/implementation-of-a-c-counter-for-inherited-classes-that-is-visible-in-default

Content Attribution

This content was originally published by pohaha at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: