A Smart Pointer Class for Subclass Pointers | CodeGuru

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 {… […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 9, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.