如何使用智能指针围绕 C“对象"实现包装器? [英] How to implement a wrapper around C 'objects' using smart pointers?
本文介绍了如何使用智能指针围绕 C“对象"实现包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个 C 库,它使用来自 C++ 的原始指针.因此,我正在考虑将所有指向 C 对象的指针包装在 C++ 类中并将它们转换为智能指针.我已经建立了一个工作示例:
I am using a C library which uses raw pointers from C++. Therefore, I'm looking into wrapping all the pointers to C objects in C++ classes and turning them into smart pointers. I've built a working example:
#include <iostream>
using namespace std;
// the C library is oop: using structs and naming conventions. Like this:
// C library declarations
struct Animal_s {
int age;
};
typedef struct Animal_s Animal;
Animal* make_animal(int age);
void free_animal(Animal* animal);
Animal* do_something_with_animal(Animal* animal);
// C lib implementations
Animal* make_animal(int age ){
auto* animal = (Animal*) malloc(sizeof(Animal));
animal->age = age;
return animal;
}
void free_animal(Animal *animal){
free(animal);
}
Animal* do_something_with_animal(Animal* animal){
//...
return animal;
}
// C++ wrapper
class AnimalWrapper{
Animal* animal_; // how to use smart pointers?
public:
explicit AnimalWrapper(int age){
animal_ = make_animal(age);
};
~AnimalWrapper(){
free_animal(animal_);
}
AnimalWrapper(const AnimalWrapper& animalWrapper){
if (this != &animalWrapper){
animal_ = animalWrapper.animal_;
}
}
AnimalWrapper(AnimalWrapper&& animalWrapper) noexcept {
if (this != &animalWrapper){
animal_ = animalWrapper.animal_;
}
}
AnimalWrapper& operator=(const AnimalWrapper& animalWrapper) {
if (this != &animalWrapper){
animal_ = animalWrapper.animal_;
}
return *this;
}
AnimalWrapper& operator=(AnimalWrapper&& animalWrapper) noexcept {
if (this != &animalWrapper){
animal_ = animalWrapper.animal_;
}
return *this;
}
Animal *getAnimal() const {
return animal_;
}
Animal* doSomethingWithAnimal(){
return do_something_with_animal(animal_);
}
};
int main(){
AnimalWrapper animalWrapper(6);
return 0;
};
这个例子有效,根据 valgrind,正确管理内存.但是,是否可以使用智能指针实现包装类?或者我必须手动管理包装类中的内存?
This example works and according to valgrind, mangages the memory correctly. However, is it possible to implement the wrapper class using smart pointers? Or must I manually manage the memory in the wrapper classes?
推荐答案
您可以使用 std::unique_ptr
用于管理 Animal
对象的生命周期:
You can use std::unique_ptr
for managing lifetime of Animal
objects:
#include <memory>
struct FreeAnimal {
void operator()(Animal* a) const noexcept {
free_animal(a);
}
};
using AnimalPtr = std::unique_ptr<Animal, FreeAnimal>;
int main() {
AnimalPtr party_animal{make_animal(21)};
do_something_with_animal(&*party_animal);
// or
do_something_with_animal(party_animal.get());
}
这篇关于如何使用智能指针围绕 C“对象"实现包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文