A Smart Pointer Class for Subclass Pointers

The SmartPtr class by Stefan Tchekanov works nicely for homogenous class objects but it does not allow assignment between base and derived class pointers. I am presenting here a simple template class that performs reference counting garbage collection and supports subclass pointers. The example that follows explains the use of this class:

class B {
... ...
};

class A : public B {
A(const char* s1, const char* s2);
void foo() { ... }
... ...
};

P<B> a = new A("Smart", "Pointer");
P<B> b = a;
P<A> c(a.Reference());
a = 0;
c->foo();

In this example, a derived class (A) object pointed by a base class (B) smart pointer a is assigned back to c -- a smart pointer to the derived class (A). The reference counting and automatically object destruction is similar to other smart pointer implementations (e.g. Stefan's RefCountPtr), but in our case it allows access derived class members (which do not have to be overloaded virtual functions) from a base class smart pointer.

However, this added flexibility comes at a cost. The assignment from pointers a to c in the example above does not conduct type checking. When "down casting" a smart pointer from super (base) class to subclass (derived) class, it is your responsibility to make sure that the pointed object is indeed a subclass object. If anyone out there has a fix for this drawback, please post.

Downloads

Download the demo project - 4 Kb
Download the source - 1 Kb



Comments

  • Virtual Pointers

    Posted by Legacy on 03/21/2002 12:00am

    Originally posted by: Eric Thorbjornsen

    In your source code, you have the following lines:
    
    

    P<A> y(x.Reference()); // y is the 2nd reference

    y->print(); // Strange enough, this does not crash
    // but B::print() was called (MSVC 6.0).
    // Any idea?

    Not sure if you have figured this out by now or not,
    but this is the correct behavior. By allocating a base
    class object B and then casting to the derived class
    object A, all of the virtual calls will access the base
    class virtual methods in B. To simplify your example,
    try this on any C++ compiler:

    #include <stdio.h>
    class B
    {
    public:

    virtual void print() { printf( "class B\n" ); }
    };

    class A : public B
    {
    public:
    void print() { printf( "class A\n" ); }
    };

    int main()
    {
    B* b = new B;
    b->print();

    A* a = (A*)b;
    a->print();

    return 0;
    }

    It's a good interview question ;-)

    As far as the other issue, I'm not sure if there is a way
    to tell if you are correctly casting or not.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Enterprise cloud adoption has evolved rapidly from fringe curiosity to the mainstream. As enterprises increasingly move mission-critical workloads to the cloud, it's important to track best practices to ensure a seamless migration process. While CIOs are becoming increasingly mature and pragmatic in their approach to cloud, surprises and challenges still need to be addressed. Read this eBook to learn the key best practices for cloud deployment success, the importance of SLAs in choosing a cloud provider, and …

  • Here are the five core principles for monetizing your storage like never before and winning more customer applications than ever for your cloud and hosted services. These are your keys to the next generation data center.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date