A Smart Pointer Class for Subclass Pointers

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • Digital transformation is a growing, urgent imperative for organizations worldwide. Subsequently, network resources to support digital initiatives are coming under intense scrutiny. Increasingly, that focus concentrates on the wide area network (WAN), which provides essential connectivity and valuable network services for branch offices, remote sites, and mobile workers. This white paper from IDC examines how digital transformation and cloud computing have necessitated a significant reassessment of the WAN, …

  • You know that visibility is critical to security. You simply can't secure what you can't see. Read this Gartner research note to learn why visibility through a continuous adaptive risk and trust assessment (CARTA) approach is key to securing digital business. Download now to learn eight ways this approach can be deployed to optimize the visibility into your organization's cloud services, mobile endpoints, and digital ecosystems.

Most Popular Programming Stories

More for Developers

RSS Feeds

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