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

  • Despite the popularity of cloud software, many companies are reluctant to move their contact centers to the cloud. This white paper breaks down cloud architecture, the benefits of cloud and the difference between cloud and on-premise systems. It's a helpful resource for companies who are curious about cloud contact center software but have reservations. It offers practical explanations, extensive research and a guide to selecting the right cloud contact center vendor.

  • Like many roles in the C-Suite, the CIO role is also changing. Its impact and control have been circumvented by cloud computing, the consumerization of IT, and employees that now have unprecedented access to technology. The role of the CIO is at a critical juncture. For some CIOs, the changing landscape reduces their scope of influence. For others, it means unprecedented opportunity. This eBook explores the changes that CIOs are experiencing today and how these executives can leverage their expertise to help …

Most Popular Programming Stories

More for Developers

RSS Feeds

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