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;

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.


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


  • Fake Oakley Plain Glass free shipping

    Posted by fjpvusgqw on 06/25/2013 05:01pm

    Cheap Ray Ban Sunglasses Sale ,Today, Oakley, sunglasses are already more than simply a power tool to bar sunshine, but additionally plays another role - decoration. The main products include women's Oakley sunglasses, ski goggles, motocross goggles, golf series, leisure series, special number of five major categories, in addition to hats, T-shirts, handbags along with peripheral products. Oakley Sunglasses Outlet ,Oakley Sunglasses lenses are very safe, no pressure, may get a new optical pause, which means you has to be truthful and accurate vision of premium clarity. The mixture of Oakley shade selection is quite large, including the various exquisite design. Oakley's main objective should be to provide them with the prospect in the chance of various kinds of range is fully satisfied. Cheap Oakley Mens ,It is possible to difference between and ordinary OAKLEY sunglasses discount? That is a various sizes, shapes and fashions to indicate off your attitude, actually talking to you, in order to echo your personality. Irrespective of closest to the aesthetic principle of Leonardo da Vinci oval face, round face, triangular face, square face, a rectangular face, do not concern yourself about Oakley sunglasses will satisfy you. How long anybody incorporates a pair or two hiding of their luggage and vehicles and also private collections and fashion sunglasses? Oakley sunglasses selling around the world, are widespread in popular sunglasses, Oakley sunglasses, fashion, sport and health, to maintain the strong sunlight, and away from you. If you are wanting to hone the top for your specific sport or Applied Optics, take a look at the optional lens just for this high-quality performance sunglasses set. Artists and professional sunglasses, equally expensive to the kind of movement, more(a) the price of the vast majority of sunglasses. However, you will discover a variety of discount Oakley sunglasses available on the internet. Elegantly simple Oakley sunglasses will keep all artwork by designers to make net cup. Oakley sunglasses, material selection, production process or function, design models, within the forefront with the market, is not inferior for the international high-end brand. Simply because these are adequately designed wonderful gallons addition, another necessary point, why Oakley sunglasses include the rights of girls, in reality, not. Purchase on the internet is very simple, just give a two of glasses, you need to the shopping basket and pay using PayPal or credit-based card, after which, the organization will make sure that your particular new glasses have been in perfect condition.

  • 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

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

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

    int main()
    B* b = new B;

    A* a = (A*)b;

    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.

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

Top White Papers and Webcasts

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

  • Remember getting your first box of LEGOS as a kid? How fun it was putting the pieces together, collaborating with your friends to create something new? Now, as an IT professional, assembling and maintaining a Lego-like collaboration infrastructure isn't what you signed up for. Piecing together disparate systems of record for email, web meetings and other applications is about as painful as stepping on a pile of Legos. Download the e-book to learn how implementing a collaboration system connects systems of …

Most Popular Programming Stories

More for Developers

RSS Feeds

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