我一直在从事教授所完成的任务,在该任务中,我必须解决可变参数模板类。问题是,我无法在递归构造函数中修改类成员。我不知道为什么会这样,一旦进入下一个构造函数调用,它将丢弃我对变量的更改。
我尝试过的
使用指针int* count代替int count
使用设置器来设置 counter
我已经在Google搜寻了几个小时,但找不到解决方法。
头文件“ test.h”:
#include <cstdarg>
#include <iostream>
class Counter {
private:
int count = 0;
int tmp;
public:
template <typename... Rest> Counter (int t, Rest... rest) {
count++;
std::cout << "start recursive number " << count << "...\n";
Counter(rest ...);
tmp = t;
std::cout << "end recursive number " << count << "...\n";
}
Counter (int t) {
count++;
tmp = t;
std::cout << "reached end of recursive -> " …Run Code Online (Sandbox Code Playgroud)