gpt4 book ai didi

c++ - 在默认构造函数中可见的继承类的 c++ 计数器的实现

转载 作者:行者123 更新时间:2023-12-02 10:26:20 25 4
gpt4 key购买 nike

所以我尝试了多种方法为我的类创建一个在构造函数中传递数字的计数器
我希望它输出如下内容:

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
我在哪里得到这个:
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
注意用户提示输入值的区别,这些值是从 new 调用的构造函数调用的。运算符(operator)
ofc 需要给他们单独的名字,这也是我想解决的问题。您可能提出的任何建议都是可以接受的。
主要目标是在 new 时立即实例化对象。操作符在没有进入 for 的情况下被调用我 ofc 可以调用的循环 "The (A/B) # (i)"这是我使用过的代码:它使用“奇怪的重复模板模式”(顺便说一句,这个名字是一个杀手!)
//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;
}

最佳答案

CRTP 与虚拟多态性并不能很好地融合 - 虚拟多态性涉及一个公共(public)基类,而 CRTP 为每个派生类型生成一个不同的基类。
无论如何,你可以用组合来破解它。这是带有工厂方法模板的非模板版本的计数器。请注意,要进行准确的引用计数,必须遵循 3 规则:

class counter
{
public:
template <typename T>
static counter create()
{
static unsigned long count = 0;
return { &count };
}
counter(const counter& that) : count_addr(that.count_addr)
{
(*count_addr)++;
}
counter& operator =(const counter& that)
{
(*count_addr)--;
count_addr = that.count_addr;
(*count_addr)++;
return *this;
}
~counter()
{
(*count_addr)--;
}
unsigned long GetInstancesCount()
{
return *count_addr;
}

private:
unsigned long* count_addr;
counter(unsigned long* count_addr) : count_addr(count_addr)
{
(*count_addr)++;
}
};
然后,您可以将计数器作为基类的成员嵌入,派生类可以选择通过 protected 构造函数提供自己的计数器。公共(public)构造函数转发工厂方法而不是计数器的位是确保没有事件的临时拷贝,这可能会意外地增加计数,当 ObjectNumber被安排了。
class base
{
public:
base() : base(counter::create<base>)
{
}
int get_count()
{
return ctr.GetInstancesCount();
}
private:
counter ctr;
protected:
base(counter(*ctr)()) : ctr(ctr()), ObjectNumber(this->ctr.GetInstancesCount())
{
std::cout << "The A# " << ObjectNumber <<": " << std::endl;
}
unsigned long ObjectNumber; // important - must come after counter
};

class derived : public base
{
public:
derived() : base(counter::create<derived>)
{
}
};
之后,我可以大致复制您的预期输出: https://godbolt.org/z/eonfb9

关于c++ - 在默认构造函数中可见的继承类的 c++ 计数器的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64396538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com