Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:CRTP:如何知道所有对象的计数?_C++_Crtp - Fatal编程技术网

C++ C++;:CRTP:如何知道所有对象的计数?

C++ C++;:CRTP:如何知道所有对象的计数?,c++,crtp,C++,Crtp,我需要你们的帮助(可能是基本问题,抱歉)在CRTP。 这是基于以下职位: 使用CRTP,我们可以计算为每个类类型创建的对象的数量。但是,使用这种方法,我无法找到在系统中创建的对象总数 有没有办法做到这一点 提前谢谢 问候,, SNR让所有类都从一个保留计数器的公共基类继承,类似 class Counter { static int count = 0; protected: Counter() { count++; } public: int getCount() { retu

我需要你们的帮助(可能是基本问题,抱歉)在CRTP。 这是基于以下职位:

使用CRTP,我们可以计算为每个类类型创建的对象的数量。但是,使用这种方法,我无法找到在系统中创建的对象总数

有没有办法做到这一点

提前谢谢

问候,,
SNR

让所有类都从一个保留计数器的公共基类继承,类似

class Counter {
  static int count = 0;

protected:
  Counter() { count++; }

public:
  int getCount() { return count; }
};
由于要计算所有实例,不需要区分不同的派生类,因此不需要CRTP


如果您想要两个计数,您可以使用上面的类作为CRTP类的基类。

此解决方案包含两个(每类和全局)计数器的代码

extern size_t Global_counter; //In .cpp file define it: size_t Global_counter = 0;

template <class T>
class CountedClass
{
protected:
  static size_t this_class_counter;

public:
  static size_t GetThisClassCounter()
  {
    return this_class_counter;
  }

  static size_t GetGlobalCounter()
  {
    return Global_counter;
  }

  CountedClass()
  {
    ++this_class_counter;
    ++Global_counter;
  }
};

template <class T>
size_t CountedClass<T>::this_class_counter = 0;
extern size\u t Global\u计数器//在.cpp文件中定义它:size\u t Global\u counter=0;
模板
类计数类
{
受保护的:
此类计数器的静态大小;
公众:
静态大小\u t GetThisClassCounter()
{
返回该类计数器;
}
静态大小\u t GetGlobalCounter()
{
返回全局计数器;
}
CountedClass()
{
++该类计数器;
++全球_计数器;
}
};
模板
大小\u t CountedClass::此\u类\u计数器=0;
示例用法:

class A : public CountedClass<A>
{
public:
  A() : CountedClass<A>()
  {
  }
};

class B : public CountedClass<B>
{
public:
  B() : CountedClass<B>()
  {
  }
};

A a1;
A a2;
B b2;

std::cout<<A::GetThisClassCounter(); //prints 2
std::cout<<A::GetGlobalCounter(); //prints 3

std::cout<<B::GetThisClassCounter(); //prints 1
std::cout<<B::GetGlobalCounter(); //prints 3
A类:公共计数类
{
公众:
A():CountedClass()
{
}
};
B类:公共计数类
{
公众:
B():CountedClass()
{
}
};
A a1;
A a2;
B b2;

std::coutPlease让问题变得独立。您是编写单线程还是多线程?@Mateusz Grzejek,我想从现在开始为单线程编程。但未来的需求可能是多线程的。正如您所说,这是为了维护对象配置文件信息,并希望添加对象大小和任何其他与对象相关的信息。目前(仅计数),您只需将计数器的增量更改为原子操作(通过std::atomic[C++11]、_InterlockedIncrement()[Windows]或某些GCC内部函数[Linux])。如果为每个类存储的内容增加,则需要确保多个线程可以在没有冲突或锁定的情况下对其进行写入(可能使用std::mutex[C++11]、CriticalSection[Windows]ot pthread的mutex[Linux])。如果是多线程,则应在提供的示例中使用@MateuszGrzejek's answer中的计数器变量,每个类都已经从
Counted
类继承,因此您的解决方案引入了多重继承,这对于这样简单的任务来说是不必要的。因为这只是个人资料信息,所以应该选择非侵入性的方法。我不建议使用多重继承,请仔细阅读。与使用全局变量相比,使用像我这样的类作为类模板(如示例中的类模板)的基类并不是更具侵入性和更好的封装。如果你不想侵入性地使用内存分析器,不要使用基类。@Nicola Musatti,非常感谢你的代码。我在寻找一个类,它可以告诉我们什么时候用它的类名创建和销毁派生类对象(真的想用对象名)。因此,qn从第一个链接开始用于对象分析。如果我想为一个对象保存这些信息,我觉得CRTP很好。Bcz,有了CRTP,我可以得到类名,可以说它是什么时候创建的,被破坏的,以及该类的大小。+1很好的解决方案!如果您从类中找出
GetGlobalCounter
的getter(应该不会太难),那就更好了。谢谢。如果要从类中删除GetGlobalCounter(),这可以毫无问题地完成,只需将其设为全局函数即可。然而,我之所以把它放在这里,是出于一个原因——看看示例代码,它看起来更直观。您将全局计数器视为每个类的一部分,而不是一个独立变量。但这是一个品味的问题,我认为:)可能是,为这种情况引入某种类别特征类别访问点可能是有用的。