Wrapping around old C Structures with smart pointers in C++11 and auto-freeing them

后端 未结 2 1425
南方客
南方客 2021-01-21 23:58

I\'m using Word-Net, an old C library developed by Princeton University back in the nineties. The library is written in C, and only reveals the headers but not its actual implem

2条回答
  •  独厮守ぢ
    2021-01-22 00:44

    You can use std::shared_ptr for this purpose, as you can provide the deleter to use to free the pointer.

    std::shared_ptr findTheInfo(...) {
        std::shared_ptr sp(findtheinfo(...), free_syns);
        return sp;
    }
    std::shared_ptr tracePtrs(...) {
        std::shared_ptr sp(traceptrs(...), free_synset);
        return sp;
    }
    

    Now, if they really represent different things, you might want to spend a bit more effort and provide two types that wrap each use and provide the appropriate interface. Does it make sense to treat both as the same type when transversing a list and a tree might be completely different?

提交回复
热议问题