简体   繁体   中英

How to begin writing a smart pointer?

I've just received a task in my university, which is to write a smart pointer. I received a skeleton, and I need to implement the needed methods. If I'm right, a smart pointer is a type of pointer which counts the reference numbers to a given object (?) and, if that counter reaches zero, it deletes the given object.

This is the skeleton:

template<class T>
class my_pointer {
public:       
    my_pointer();
};

class refcounted {
    /* the reference counted types should have the public interface that is defined here */
public:     
    int incRefCnt();
    int decRefCnt();
};

int main() {
    my_pointer<refcounted> obj1 = new refcounted();
    my_pointer<refcounted> obj2 = obj1;

    return 0;
}

Firstly, what should this line do? refcounted is not a child of my_pointer, so how can I possibly instantiate a new refcounted object and reference it with a my_pointer object (pointer?)?

my_pointer<refcounted> obj1 = new refcounted();

Why isn't there a counter in the my_pointer class, and why is the counter in the refcounted class? How should I start with this? I'm not really good in c++. Thanks in advance!

Expanding my comment into an answer:

First question: what the line is supposed to do is create a new object of type refcounted on the heap, and then use an implicit converting constructor to create a smart pointer that points to this object, incrementing its internal reference count.

Second question: It appears you're supposed to implement an intrusive ref-counted smart pointer, where the reference count is stored within the pointed-to object. That's what the two ref count functions do. Intrusive reference counting has the advantage of being more space-efficient (since you do not need to allocate the reference count separately), and that you can take a raw pointer and simply turn it into a smart pointer, without caring about other smart pointers that already exist. It has the disadvantage of being unable to point to arbitrary types, and not supporting weak pointers.

Third question: You start by thinking about what it means to create, copy, and destroy a smart pointer.

Firstly, what should this line do?

my_pointer<refcounted> obj1 = new refcounted();

That line should initialize a my_pointer<refcounted> smart pointer from the refcounted raw pointer passed to it.

how can I possibly instantiate a new refcounted object and reference it with a my_pointer object (pointer?)?

You can do it using a converting constructor . my_pointer doesn't have a constructor accepting a T* , so it seems to have been left for you to define.

Why isn't there a counter in the my_pointer class, and why is the counter in the refcounted class?

You're apparently tasked with creating an intrusive pointer that expects the stored object to do the reference counting.

How should I start with this?

I would take a glance at the description and interface of boost::intrusive_ptr . You probably don't need quite as complete interface for your assignment, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM