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

  • Enterprises today must focus on digital transformation to remain competitive or disrupt their industries. The foundation for successful transformation is the adoption of a cloud-first mindset. However, IT organizations must first address legacy infrastructure and fragmented management tools that were not designed for the speed and flexibility of the cloud and digital era. Read this IDC Technology Spotlight paper to explore: Why digital transformation is driving a shift to a cloud-centric enterprise Key …

  • On-demand EventEvent Date: March 21, 2017 With the rise in ransomware attacks, bank heists that reached new levels of sophistication, and extortion plots that were beyond anything we could have imagined, 2016 certainly was an eventful year for cybersecurity. Going into 2017, no business can afford to be uninformed about cybersecurity or unprepared for an attack. Watch this informative webinar, presented by Andrey Pozhogin, Senior B2B Product Marketing Manager at Kaspersky Lab, as he examines predictions for …

Most Popular Programming Stories

More for Developers

RSS Feeds

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