How to indicate ownership of a pointer in C ++ a pointer

Let's say I have a class:

class Scheduler {
    Scheduler(JobService *service);
    AddJob(JobID id, ISchedule *schedule);
}

      

The constructor takes a pointer to the service, but the Scheduler doesn't take ownership of the service pointer. The service pointer is assumed to be released by the caller.

The AddJob case is the opposite. Scheduled hours are controlled by the scheduler, and when the job is no longer needed, the schedule starts.

From an API perspective, it is not clear who takes responsibility for the pointer and who does not. I know if there are any methods that indicate intent through API design and not through documentation. To make it a little more silly and obvious.

If I could, I would build an instance of ISchedule, but this is an abstract class in C ++ (interface) and so it would be impractical to create add-ins for each type of schedule. So, I have to take a pointer in Add.

+3


source to share


3 answers


You have no parameters (other than clear documentation) to indicate ownership of the raw pointer.

What smart pointers from the C ++ dynamic control library are for:

  • std::unique_ptr

    transfers ownership to the recipient
  • std::shared_ptr

    ownership of shares among owners
  • std::weak_ptr

    indicates dependent share



And as pointed out in @Galik's brilliant answer , a dedicated link can be used to specify a strict lifespan dependency.

+4


source


The number of scenarios is more than two.

class Scheduler {

    // pass the raw pointer (or use a reference) to expresses
    // no ownership transfer (The passed in object will no longer be  
    // needed after the pointer or reference becomes invalid)
    Scheduler(JobService* service); 
    Scheduler(JobService& service); 

    // use a std::unique_ptr to pass ownership
    AddJob(JobID id, std::unique_ptr<ISchedule> schedule);

    // use a std::shared_ptr to pass shared ownership
    // when the passed in object needs to outlive either the caller
    // or the receiver and either one may need to delete it
    SomethingElse1(std::shared_ptr<Stuff> stuff);


    // use a std::weak_ptr to pass shared ownership
    // when the object may, or may not outlive
    // the receiver and the receiver needs to be able to detect
    // if the pointer is still valid (like an intermittent service)
    SomethingElse2(std::weak_ptr<Stuff> stuff);
};

      

Literature:



R.30 Make smart pointers as parameters only to explicitly express the semantics of life

R.32 Take the unique_ptr parameter to express that the function assumes ownership of the widget

R.34 Take the shared_ptr parameter to express that the function owns the part

+3


source


Transfer std::unique_ptr<ISchedule>

is an idiomatic way of transferring ownership of an object. So this is the right way to go AddJob

.

Passing a raw pointer means that ownership is not being transferred.

Obviously std::shared_ptr

indicates sharing.

+2


source







All Articles