CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Deriving from shared_ptr

    Is it safe to publicly derived from shared_ptr? I don't see a virtual destructor. . .
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Deriving from shared_ptr

    No, the lack of virtual members functions and a non-virtual destructor show that shared_ptr is not intended to be a base class.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2007
    Posts
    811

    Re: Deriving from shared_ptr

    Also, I would ask what functionality you perceive you need to derive new class from it?
    If it's has something to do with deletion of an object, shared_ptr already prides option to use your custom deleter.

  4. #4
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: Deriving from shared_ptr

    I was toying around with the idea of a derived version of the shared_ptr that automatically dynamically allocated space using a default contructor of the object is pointed to. That way I could write:

    myshared_ptr<MyClass> pMyClass;

    instead of. . .

    shared_ptr<MyClass> pMyClass(new MyClass);

    The idea was to make my code a little less cluttered.

    Oh well.
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  5. #5
    Join Date
    Feb 2003
    Posts
    377

    Re: Deriving from shared_ptr

    If you wanted to you could achieve the same thing through composition or private inheritance.

  6. #6
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: Deriving from shared_ptr

    Why is private inheritance different?

    What do you mean by composition?
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  7. #7
    Join Date
    Feb 2003
    Posts
    377

    Re: Deriving from shared_ptr

    Public inheritance is generally meant to be used to implement polymorphic behavior. It models "is-a" or "works-like-a". In other words, you have code that works on a base class pointer or reference, and virtual functions allow derived classes to specialize the work done by the functions that are called.

    Private inheritance is used for "has-a" or "is-implemented-in-terms-of". It is a way of implementing a class in terms of another class. Composition is another way of doing this which is usually preferred, but there are a few cases in which private inheritance makes more sense. Composition just means making a member variable out of the other class:
    Code:
    template<typename T>
    class myshared_ptr
    {
        shared_ptr<T> p;
    public:
        myshared_ptr() : p(new T) { }
    
        // add forwarding functions here.
    };
    See this FAQ for more information: http://www.parashift.com/c++-faq-lit...ce.html#faq-24

  8. #8
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: Deriving from shared_ptr

    I know what private inheritance is, but I'm not sure how it gets around the 'no virtual destructor' problem. It also presents the problem of hiding the shared_ptr<T> interface. I'd prefer not to bother access declarations to get around this.

    *sigh* I think this might be more work than necessary at the moment. Thanks for the suggestion anyway.
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  9. #9
    Join Date
    Feb 2003
    Posts
    377

    Re: Deriving from shared_ptr

    It gets around the problem because code that uses your class cannot attempt to use it polymorphically, and specifically cannot delete it through a pointer to the base shared_ptr class.

    Admittedly, this will probably not be a problem for you, since I doubt a shared_ptr is allocated with new anyway. However, using one of the other two alternatives is not a lot of work and makes your design intent clearer. Since shared_ptr has a relatively small and defined interface, I don't think it would be that hard to add forwarding functions (or using statements if you use private inheritance).

    I honestly don't know whether it is a good idea to make your own smart pointer just for the minor convenience of simpler construction, but it isn't that hard if you really wanted to.

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Deriving from shared_ptr

    I have a feeling you need a factory function returning a shared_ptr. But I also have a feeling that you are uselessly wondering about saving some typing via artificial ways. The derivation from shared_ptr for the use case you provided is not very good/useful. But if the object creation is complex or you need to do some fancy stuff with the created object you might choose a creational pattern to save yourself the same typing repeatedly and avoiding the complexity by keeping it at one place and reducing chances of bugs/different behavior.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured