c++pointersreturncommand-pattern

Returning a pointer instance variable in C++


I have a command pattern class with a pointer to another object (let's call it Duck). Its subclasses will use Duck, but to control and track when Duck is used (for debugging) I made Duck private and subclasses can only get it through a method getDuck();

class Command
{
    private:
        Duck* target;
    public:
        //Parametrized Constructor
        Command(Duck* _target);
        Duck* getDuck() { return target; }

        ...
};

I recently learned in Effective C++ by Scott Myers the horrors of returning a pointer. I tried just returning the dereferenced version, but that caused a copy of target to be returned. Should I (and is there a way to) simply return a Duck object instead of a pointer to it?


Solution

  • Conceptually, there are two kinds of pointers: owning and non-owning.

    An owning pointer indicates that the holding object/function owns the allocation and is responsible for deleting the object.

    A non-owning pointer is a "borrowed" object; the holding object/function does not own the object and is not responsible for cleaning it up.

    Ideally, this manifests in the C++ type system since C++11 in the following ways:

    There is nothing inherently wrong with returning a raw pointer, though I would only return a pointer when the function needs to conditionally give access to an object it owns:

    Note that not all standard library facilities will respect these guidelines; indeed not all of them can! new T must return a T * because that's how it's defined to work. These are guidelines that you should follow in your application.