我用构造函数创建了一个类。
我正在main.cpp
中创建 n个个对象。现在,每次创建对象时,都应自动调用构造函数。
但是由于我正在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)(会话*)”的调用不匹配
Foo [count](新会话(计数));
有什么建议吗?
答案 0 :(得分:1)
您不需要删除unique_ptr,当变量超出范围时,它们将被销毁。在这种情况下,它将是主要功能的结束。
这就是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