|
-
May 5th, 2008, 06:47 PM
#7
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|