Click here to Skip to main content
12,758,300 members (34,722 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads


108 bookmarked
Posted 13 Jun 2004

Implementation of a B-Tree Database Class

, 29 Jun 2006
An article and source code regarding the implmentation of B-Trees in C++.
 smartptrs.h  -  Header file that contains the definition and inline
                 implementation of smart pointers. This is not entirely
                 my code ... most of it was lifted from the MSDN. Search
                 for "refcount smart pointers" and view the "Templates and
                 Smart Pointers" article there.
                 What did I do? I added the RefCount::lessthan function and
                 the Ptr::operator < operator. I also allow the assignment
                 of const T* by casting it to non-const. This was required
                 so that the smart pointers could be stored in std::vectors,
                 lists, maps, etc. Finally, I added a few more comparison

 begin     : April 2004
 copyright : (C) 2004 by Phil Cairns
 email     :

 This code may be used in compiled form in any way you desire (including
 commercial use). The code may be redistributed unmodified by any means
 providing it is not sold for profit without the authors written consent,
 and providing that this notice and the authors name and all copyright
 notices remains intact.

 This software is provided "as is" without express or implied warranty. Use
 it at your own risk!

#if !defined(__smartptrs_h)
#define __smartptrs_h

namespace Database
	Base class of smart pointer classes.
	class RefCount
		int _crefs;

		RefCount() : _crefs(0) {}
		virtual ~RefCount() {}
		virtual void upcount() { ++_crefs; }
		virtual void downcount(void)
			if (--_crefs == 0)
				delete this;
		virtual bool lessthan(const RefCount* other)
			return this < other;

	Smart pointer template.
	template <class T> class Ptr
		T* _p;

		Ptr(const Ptr<T>& ptr) : _p(ptr._p) { if (_p) _p->upcount(); }
		Ptr() : _p(0) {}
		Ptr(T* p) : _p(p) { if (_p) _p->upcount(); }
		~Ptr(void) { if (_p) _p->downcount(); }
		operator T*(void) const { return _p; }
		T& operator*(void) const { return *_p; }
		T* operator->(void) const { return _p; }
		Ptr& operator=(const Ptr<T> &p) { return operator=((T*)(Ptr<T>)p); }
		Ptr& operator=(T* p)
			if (_p) _p->downcount();
			_p = p;
			if (_p) _p->upcount();
			return *this;
		Ptr& operator=(const T* p)
			return operator=((T*)p);
		bool operator ==(const Ptr<T> &p) const { return _p == p._p; }
		bool operator ==(const T* p) const { return _p == p; }
		bool operator !=(const Ptr<T> &p) const { return _p != p._p; }
		bool operator !=(const T* p) const { return _p != p; }
		bool operator < (const Ptr<T> &p) const { return _p->lessthan(p._p); }

	typedef Ptr<RefCount> RefCountPtr;


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Australia Australia
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 29 Jun 2006
Article Copyright 2004 by _oti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid