Skip to main content

Posts

Showing posts with the label memory management

C++ Smart Pointer

The following is the finalized code for a simple C++ smart pointer as demonstrated by my youtube video:  here Usage: void main(){      ...     Pointer<ObjectType> pointer(new ObjectType);     ... } /*  * Pointer.h  *  *  Created on: Mar 16, 2012  *      Author: ukaku  */ #ifndef POINTER_H_ #define POINTER_H_ #include <iostream> using namespace std ; /**  * Reference represents a counter that will be incremented or decremented as  * the number of references to a pecticular pointer is made;  *  * where data means: a symbol used to point to, or associate to another object  */ class ReferenceCounter { private :     int counter ; public :     ReferenceCounter ( ) {         counter = 0 ;   //initially set to zero     }     void increase ( ) {         counter ++;   //increase it     }     int decrease ( ) {         -- counter ;   //decrease it         if ( counter < 0 ) {             cout << "ReferenceCouner is <