c++ - 如何使用 unique_ptr 声明调用构造函数并处理程序终止信号
问题描述
我创建了一个带有构造函数的类。
我正在创建n 个对象main.cpp
。现在随着每个对象的创建,构造函数应该被自动调用。
但由于我在 中创建此对象main.cpp
,我想使用信号来处理“Ctrl+C”终止。
我写了main.cpp
这样的:
#include <iostream>
#include "Session.hpp"
class Session {
public:
Session(int _count):count(_count) {
std::cout << "Create Session " << count << std::endl;
}
~Session() {
std::cout << "Destroy Session " << count << std::endl;
}
Print() {
cout << "Hello" << endl;
}
private:
const int count;
};
void signal_handler(int signal, unsigned int count, **WHAT SHOULD I WRITE HERE**) {
for (unsigned int del_count = 0; del_count < count; del_count++) {
**I WANT TO DELETE ALL THE FOO OBJECTS CREATED IN THE MAIN FUNCTION**
}
}
int main() {
unsigned int num_of_sessions;
cin >> num_of_sessions;
signal(SIGINT, signal_handler, num_of_sessions, **WHAT MORE SHOULD I PASS HERE**);
unique_ptr<Session> Foo[num_of_sessions];
unsigned int count = 0; // initialize the counter for sessions
while (count < num_of_sessions) {
Foo[count] (new Session(count));
count++;
}
while (true){
for (count = 0; count < num_of_sessions; count++) {
Foo[count]->PrintName();
}
}
return 0;
}
我收到此错误
错误:不匹配调用 '(std::unique_ptr) (Session*)'<br> Foo[count] (new Session(count));
有什么建议吗?
解决方案
你不需要删除 unique_ptr,它们会在变量超出范围时被销毁;在这种情况下,它将是 main 函数的结尾。
这就是 unique_ptr 的意义所在,您不必关心内存管理。
如果您只是想设置一个信号并对主函数内部堆栈中分配的对象执行操作,您可以使用如下指针:
#include <iostream>
#include <csignal>
#include <vector>
#include <atomic>
#include <memory>
#include <chrono>
#include <thread>
std::atomic<bool> end_condition;
class Session {
public:
Session(int _count):count(_count) {
std::cout << "Create Session " << count << std::endl;
}
~Session() {
std::cout << "Destroy Session " << count << std::endl;
}
void printSessionCount() {
std::cout << "Session count is " << count << std::endl;
}
private:
const int count;
};
std::vector< std::unique_ptr<Session>>* foo_p; // <= Probably not necessary but this is how you would access your variable defined in main in signal handler
void signal_handler(int signal) {
// You don't have handle clean up of your vector.
// foo_p->clear(); <= Unnecessary to delete every element of your vector here, since they will be automatically deleted at the end of main
end_condition.store(false);
}
int main() {
end_condition.store(true);
unsigned int num_of_sessions;
std::cin >> num_of_sessions;
// register signal SIGINT and signal handler
signal(SIGINT, signal_handler);
std::vector< std::unique_ptr<Session> > foo;
foo_p = &foo; // Make your global pointer points to your pointer created in the main function. Accessing foo_p before the point will result in a segmentation fault since the pointer will be null
// You may not need this at all depending on what you want to do during in the signal handler
for(int i = 0; i < num_of_sessions; ++i) {
// Populate your vector
foo.emplace_back(new Session(i));
}
while (end_condition.load()){
//Call a function for each vector
for(int i =0; i < foo.size(); ++i) {
foo.at(i)->printSessionCount();
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
} // vector foo memory will be deleted here
推荐阅读
- coq - 如何在假设中处理 (let := _ in _)
- python - 获取错误识别器.train(x_train,np.array(y_labels))TypeError:src不是numpy数组,也不是标量
- c# - 将链接存储到变量而不是文本文件中
- python - 如何在熊猫中仅重置第一级 MultiIndex
- c# - C# 错误提供程序不适用于 groupbox 和 tabcontrols 中的文本框
- java - 调用方法。我没有得到预期的输出
- c# - 在不丢失顺序的情况下转换为 JSON 数组
- python - 生成过滤的二元笛卡尔积
- node.js - 如何在 express js 和 nodejs 中使用参数化 url - 不工作
- flutter - 在 null 上调用了 getter 'text'