Thread: Suggestion Smart Pointer Class Implementation

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Suggestion Smart Pointer Class Implementation

    Hi All

    I have created a smart pointer implementation. Below is the code. Could you guys have a look and suggest that approach is correct or not.
    One point I want to mention that, for count I have used static. On net I found that pointer variable is used.

    Can u guys please suggest why can’t we used static count?

    Below is the code:
    Code:
    #include<iostream>
    
    using namespace std;
    
    template<class T>
    class sharedPtr
    {
            T* ShrdPtr;
            static int refCount;
    
    public:
           
    
    		sharedPtr()
    		{
    		    ShrdPtr = new  T;
    
    			refCount++;
    			cout <<" \nIn Default Ctr: "<<refCount;
    		 }
    
            ~sharedPtr( )
            {
    			
                 if( --(refCount) == 0 )
                            delete ShrdPtr;
    				std::cout << "inside destructor refCount = " << refCount << "\n";
    				
    				
            }
            sharedPtr<T>& operator=( const sharedPtr& ptr );
            sharedPtr( const sharedPtr& ptr );
           // T* operator->( ) { return ShrdPtr; }
         //   T& operator*() { return *ShrdPtr; }
    };
    
    template<class T>  sharedPtr<T>& sharedPtr<T>::operator=( const sharedPtr<T>& ptr )
    {
            
            this->ShrdPtr = ptr.ShrdPtr;
            this->refCount = ptr.refCount;
    		 std::cout << endl<<"inside assignment operator="<<refCount<<endl;
            return *this;
    }
    
    template<class T>
    sharedPtr<T>::sharedPtr( const sharedPtr& ptr )
    {
           
             this->ShrdPtr = ptr.ShrdPtr;
            refCount++;
            this->refCount = ptr.refCount;
    		 std::cout << endl<<"inside copy constructor="<<refCount<<endl;
    }
    
    
     template<class T> int sharedPtr<T>::refCount = 0;
    
    int main( int argc, char* argv[] )
    {
            
            {
    			    sharedPtr<int> p ;
                    sharedPtr<int> q = p;
    				sharedPtr<int> x(q);
    				sharedPtr<int> z;
    				z = q;
    
    			
            }
    	
            return 0;
    }
    Implementation still not complete. Waiting for suggestion before proceeding.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bargi
    Can u guys please suggest why can’t we used static count?
    If you use a static member variable for the count, then it means that all the smart pointer objects will access the same count, even those smart pointers that have nothing to do with each other besides being of the same type. Therefore, once you have a second distinct smart pointer of the same type, your count will become incorrect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Ya. I got that point. In that case, if I create for object type B, then instead of having count of 1, it will show count of first type object (say first type object is type A).

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Hi I change the code, but getting issue when initializing in constructor.

    The code is crashing. I have taken pointer.

    But for every new object its intializing to 0 and program is crashing.

    Code:
    #include<iostream>
    
    using namespace std;
    
    template<class T>
    class sharedPtr
    {
            T* ShrdPtr;
            int* refCount;
    
    public:
            sharedPtr( T* ptr )
    		{
    			ShrdPtr = ptr;
    			//refCount = new int(); 
    			
    			*refCount++; 
    			cout <<" In One Parameter Ctr "<<*refCount<<endl;
    		}
    
    		sharedPtr():refCount(0)
    		{
    	        ShrdPtr = new  T;
    
    			if ( *refCount == 0 )
    			{
    		    
    			      refCount = new int; 
    		          *refCount  = 1;
    			}
    			else
    				*refCount++;
    			cout <<" \nIn Default Ctr: "<<*refCount;
    		 }
    
            ~sharedPtr( )
            {
                    
                    if( --(*refCount) == 0 )
                            delete ShrdPtr;
    				std::cout << "inside destructor refCount = " << *refCount << "\n";
            }
            sharedPtr<T>& operator=( const sharedPtr& ptr );
            sharedPtr( const sharedPtr& ptr );
           // T* operator->( ) { return ShrdPtr; }
         //   T& operator*() { return *ShrdPtr; }
    };
    
    template<class T>  sharedPtr<T>& sharedPtr<T>::operator=( const sharedPtr<T>& ptr )
    {
            //std::cout << "inside assignment operator\n";
            this->ShrdPtr = ptr.ShrdPtr;
            (*(ptr.refCount))++;
            this->refCount = ptr.refCount;
    		 std::cout << endl<<"inside assignment operator="<<*refCount<<endl;
            return *this;
    }
    
    template<class T>
    sharedPtr<T>::sharedPtr( const sharedPtr& ptr )
    {
           // std::cout << "inside copy constructor\n";
             this->ShrdPtr = ptr.ShrdPtr;
            (*(ptr.refCount))++;
            this->refCount = ptr.refCount;
    		 std::cout << endl<<"inside copy constructor="<<*refCount<<endl;
    }
    
    
    int main( int argc, char* argv[] )
    {
            sharedPtr<int> p ;//(new int);
            {
                    sharedPtr<int> q = p;
    				sharedPtr<int> x(q);
    				sharedPtr<int> z;
    				z = q;
            }
    		//getchar();
            return 0;
    }
    Can anybody help please..?

    Thanks

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My question is why are you making a smart pointer if you cannot diagnose simple errors such as dereferencing a non-initialized pointer?
    Take a hard look at where you dereference your counter and make your way backwards to find where you're initializing it.
    Your logic for operator = and the copy constructor is also wrong. The logic is wrong. (Why?)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. COM smart pointer / C++ smart pointer
    By KIBO in forum Windows Programming
    Replies: 1
    Last Post: 02-23-2011, 05:14 AM
  2. Which smart pointer to use?
    By leeor_net in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 04:29 AM
  3. smart pointer release
    By George2 in forum C++ Programming
    Replies: 24
    Last Post: 02-13-2008, 08:51 AM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. pointer and smart pointer address
    By l2u in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2006, 05:00 PM