计数在2个不同类别上创建的对象会导致问题

Counting created objects over 2 different classes leads to problems

本文关键字:对象 问题 创建 2个 同类      更新时间:2023-10-16

我想计算创建的对象。如果我使用一个类做到这一点,那很容易,但是我想从两个不同类的对象进行计数,而两个不同的计数器相互交互。简化示例:

template <class Obj>
class Counter1
{
    private: 
        static int total1;
    public:
        Counter1(){++total1;}
        .. destuctor, copy constructor ..
       static int getCounter1() {return total1;}
       static void setCounter1(int var) {total1 = var;}
};
template<class Obj> int Counter1<Obj>::total1(0);
template <class Obj>
class Counter2
{
    private: 
        static int total2;
    public:
        Counter2(){++total2;}
        .. destuctor, copy constructor ..
       static int getCounter2() {return total2;}
       static void setCounter2(int var) {total2 = var;}
};
template<class Obj> int Counter2<Obj>::total2(0);
class Test1 : private Counter1<Test1>
{
 .. some code ..
};
Test1::Test1() : Counter1()
{
   int tmp_var;
   tmp_var = Counter2<int>::getCounter2();
}

可悲的是tmp_var永远不会得到有效的数字。或换句话说,它保持为零,不会增加。我究竟做错了什么?

_________________编辑__________________
在这里,一个更详细的示例:

template <class Obj>
class Counter1
{
    private:
        static int total1;
    public:
        Counter1() {++total1;}
        Counter1(const Counter1& countObj) {if(this != &countObj) {++total1;}}
        ~Counter1() {--total1;}
        static int getCounter1() {return total1;}
        static void setCounter1(int var){total1 = var;}
};
template <class Obj> int Counter1<Obj>::total1(0);
template <class Obj>
class Counter2
{
    private:
        static int total2;
    public:
        Counter2() {++total2;}
        Counter2(const Counter2& countObj) {if(this != &countObj) {++total2;}}
        ~Counter2() {--total2;}
        static int getCounter2() {return total2;}
        static void setCounter2(int var){total2 = var;}
};
template <class Obj> int Counter2<Obj>::total2(0);
/* Memory are with a static Size with variable object Size allocator */
class Class1 : private Counter2<Class1>
{
    public:
        Class1();
        .. some methods ..
    private:
       .. some members ..
    };
/* Constructor */
Class1::Class1() : Counter2()
{
    const int varTotal1 = Counter1<int>::getCounter1();
    const int varTotal2 = getCounter2()-1;
    .. some code ..
}
.. some methods ..
class Class2 : private Counter1<Class2>
{
    public:
        Class1 _PoolsObj[TASKS_PER_PROCESS];
        Class2();
};
Class2::Class2() : Counter1()
{
    _PoolsObj[TASKS_PER_PROCESS] = {};
    /* Reset the task counter */
    Counter2<int>::setCounter2(0);
}
static Class2 class_pool[64];

我有两个问题:
1(Counter2<int>::setCounter2(0);不起作用
2(const int varTotal1 = Counter1<int>::getCounter1();始终是0

遵循新样本,以下是我的看法。首先,不要忘记模板的每个实例都有其自己的静态变量。它不像其他标准课程。那就是在您身上扮演技巧的原因。

原因

const int varTotal1 = Counter1<int>::getCounter1();

始终为0是模板counter1的实例,由class2继承。但是您要求Class1的Counter1的GetCounter1((为您提供数量或总数1的值。Class1不知道GetCounter1((。请注意,当您在示例代码中使用以下代码在counter1中实例化类时,当您实例化class2和class2知道getCounter1((时,正确递增了1个((,如果您碰巧在class2中包含getCounter1((,您可以看到Total1值。

static Class2 class_pool[64];

据我所知,以下代码有效。

Counter2<int>::setCounter2(0);

作为最后的注意,如果我是您,我只会有一个模板说计数器。按照您的正常方式使用它来控制每个类创建的对象的计数。如果您想从另一个类中查看一个类的计数,为什么不创建第三个实用程序共享类,您可以在其中存储对象的计数。或者,您可以使用Mixin根据需要组合类(使用继承和组成(,但这可能是过度杀伤。

顺便说一句,您的代码还没有完整,而您错过了定义

 TASKS_PER_PROCESS  in 
  _PoolsObj[TASKS_PER_PROCESS] = {};
   I added the following to just compile your code ..
   #define TASKS_PER_PROCESS 10

我想知道,我做错了什么,我做出了一些重大改进。

1(多亏了 @tobi303的评论,我可以删除一个计数器,而只将一个计数器用于不同的对象。

2(我的代码内部有语义错误。_PoolsObj[TASKS_PER_PROCESS] = {};完全错误。我想用0将所有数组元素杀死,但这仅将第五次称为构造函数,然后将fort(task_per_process(元素设置为0。我现在在变量声明之后放置了初始化列表以确保正确创建。

3(

public:
        Class1 _PoolsObj[TASKS_PER_PROCESS];

这当然不是不错的方法,因此我包含了一个getter功能,并将数组设置为私有。

4(我的问题是通过这两行代码的原因:

Counter1<int>::getCounter1();
Counter2<int>::setCounter2(0); 

类型不应该是int,应该是类名称:

Counter1<Class2>::getCounter();
Counter2<Class1>::setCounter(0); 

我认为我为INT而不是上课设置了反击。

在这里我的固定代码仅用于完成:

template <class Obj>
class Counter
{
    private:
        static int total;
    public:
        Counter() {++total;}
        Counter(const Counter& countObj) {if(this != &countObj) {++total;}}
        ~Counter() {--total;}
        static int getCounter() {return total;}
        static void setCounter(int var){total = var;}
};
template <class Obj> int Counter<Obj>::total(0);
class Class1 : private Counter<Class1>
{
    private:
        int member;
    public:
        int getMember();
        Class1();
};
class Class2 : private Counter<Class2>
{
    private:
        Class1 _PoolsObj[TASKS_PER_PROCESS] = {};
    public:
        Class1 getPool(int index);
        Class2();
};
Class1::Class1()
{
    const int varTotal1 = Counter<Class2>::getCounter()-1;
    const int varTotal2 = getCounter()-1;
    std::cout<<"varTotal1: "<<varTotal1<<std::endl;
    std::cout<<"varTotal2: "<<varTotal2<<std::endl;
}
int Class1::getMember()
{
    return member;
}

Class2::Class2()
{
    std::cout<<"Class2 Constructor"<<std::endl<<std::endl;
    /* Reset the task counter */
    Counter<Class1>::setCounter(0);
}
Class1 Class2::getPool(int index)
{
    return _PoolsObj[index];
}
static Class2 class_pool[64];