ฉันกำลังใช้ไลบรารี่ที่คาดว่าจะมีตัวชี้ดิบไปยังวัตถุเพื่อสร้างวัตถุบางอย่าง ฉันไม่แน่ใจว่ามันจะทำอะไรกับพอยน์เตอร์ เพื่อให้โค้ดของฉันปลอดภัยที่สุดเท่าที่จะเป็นไปได้ ฉันควรส่งอะไรไปยังฟังก์ชันนี้
- ใช้ตัวชี้เฉพาะ - หากพวกเขาตัดสินใจลบตัวชี้ ฉันจะลบสองครั้ง
- ติดตามตัวชี้ดิบ - แย่เพราะฉันต้องจำไว้ว่าต้องเขียนลบ แต่อาจเป็นการลบสองครั้ง
- ใช้ระยะเวลาอัตโนมัติและให้ตัวชี้
ให้ข้อมูลอ้างอิงแก่พวกเขา- รหัสของพวกเขาจะผิดพลาดหากพวกเขาเรียกลบ - ใช้ตัวชี้ที่ใช้ร่วมกัน - ปัญหาการลบสองครั้งเหมือนกับตัวชี้เฉพาะ แต่ตอนนี้ขอบเขตของฉันจะไม่ทำให้ตัวชี้เสียหาย
จากการอ่านของฉัน ตัวเลือกที่ 3 ดูเหมือนว่าฉันควรทำ - ไม่ควรเรียก Delete บนตัวชี้ และรูปแบบนี้บังคับใช้ แต่ถ้าฉันไม่รู้ว่าตอนนี้หรือในอนาคตพวกเขาจะเรียกลบตามข้อมูลอ้างอิงที่ฉันให้ไว้ล่ะ? ใช้ตัวชี้ที่ใช้ร่วมกันและบอกว่าไม่ใช่ความผิดของฉันเกี่ยวกับการลบสองครั้งใช่ไหม
#include <memory>
#include <iostream>
class ComplexObj {
public:
ComplexObj() : m_field(0) {}
ComplexObj(int data) : m_field(data) {}
void print() { std::cout << m_field << std::endl; }
private:
int m_field;
};
class BlackBox {
public:
BlackBox(ComplexObj* data) {
m_field = *data;
// Do other things I guess...
delete data;
std::cout << "Construction complete" << std::endl;
}
void print_data() { m_field.print(); }
private:
ComplexObj m_field;
};
int main(int argc, char* argv[]) {
// Use a smart pointer
std::unique_ptr<ComplexObj> my_ptr(new ComplexObj(1));
BlackBox obj1 = BlackBox(my_ptr.get());
obj1.print_data();
my_ptr->print(); // Bad data, since BlackBox free'd
// double delete when my_ptr goes out of scope
// Manually manage the memory
ComplexObj* manual = new ComplexObj(2);
BlackBox obj2 = BlackBox(manual);
obj2.print_data();
manual->print(); // Bad data, since BlackBox free'd
delete manual; // Pair new and delete, but this is a double delete
// Edit: use auto-duration and give them a pointer
ComplexObj by_ref(3);
BlackBox obj3 = BlackBox(&by_ref); // they can't call delete on the pointer they have
obj3.print_data();
by_ref.print();
// Use a shared pointer
std::shared_ptr<ComplexObj> our_ptr(new ComplexObj(4));
BlackBox obj4 = BlackBox(our_ptr.get());
obj4.print_data();
our_ptr->print(); // Bad data, they have free'd
// double delete when our_ptr goes out of scope
return 0;
}
คำถามอื่น ๆ ที่ฉันอ่านเกี่ยวข้องกับหัวข้อนี้...
BlackBox obj3 = BlackBox(&by_ref);
&ref
ไม่ใช่ข้อมูลอ้างอิง แต่ใช้ ที่อยู่ของref
- person churill   schedule 08.01.2021