1

Here is my code below:

template<class t>
class smart_ptr{
    t *ptr;
public: 
    smart_ptr(t *p):ptr(p){cout<<"smart pointer copy constructor is called"<<endl;}    
    smart_ptr(const smart_ptr &sm){cout<<"copy constructor is called"
                                   <<endl;ptr=sm->ptr;}   
    ~smart_ptr(){cout<<"smart pointer destructor is called"<<endl;delete(ptr);}
    t& operator *(){cout<<"returning the * of pointer"<<endl;return(*ptr);}
    t* operator ->(){cout<<"returning the -> of pointer"<<endl;return(ptr);}
    t& operator=(const t &lhs){ptr=lhs->ptr;cout<<"assignement operator called"
                                 <<endl;return *this;}

    };
    class xxx{
            int x;
    public:
            xxx(int y=0):x(y){cout<<"xxx constructor called"<<endl;}
            ~xxx(){cout<<"xxx destructor is called"<<endl;}
            void show(){cout<<"the value of x="<<x<<endl;}
    };
int main(int argc, char *argv[])
{
    xxx *x1=new xxx(50);
    smart_ptr<xxx> p2(new xxx(60));
    smart_ptr<xxx> p1(x1);
    p1->show();
    smart_ptr<xxx> p3(p2);     //calling copy construcotr is giving error
    p3=p1;                     //calling assignment operator is giving error
    p2->smart_ptr<class xxx>::~smart_ptr<class xxx>(); //calling smart pointer 
                                                        destructor gives error
    return 0;
}

I am getting compilation errors while compiling this file because of wrong copy constructor, assignment operator and destructor codes.

The errors are:

  smart_pointer_impl.cpp: In function ‘int main(int, char**)’:

  smart_pointer_impl.cpp:33: error: ‘smart_ptr<xxx>’ is not a base of ‘xxx’

  smart_pointer_impl.cpp: In copy constructor ‘smart_ptr<t>::smart_ptr(const 
  smart_ptr<t>&) [with t = xxx]’:

   smart_pointer_impl.cpp:28:   instantiated from here

   smart_pointer_impl.cpp:8: error: passing ‘const smart_ptr<xxx>’ as ‘this’ argument  
   of ‘t* 

   smart_ptr<t>::operator->() [with t = xxx]’ discards qualifiers

   smart_pointer_impl.cpp:8: error: ‘class xxx’ has no member named ‘ptr’

Please find where I am wrong in the above functions. Any help is welcome.

6
  • 4
    Could you post the errors you are getting? Commented Nov 7, 2013 at 15:24
  • 3
    sm.ptr instead of sm->ptr. operator= should be taking smart_ptr and not t. Commented Nov 7, 2013 at 15:26
  • 2
    Besides the compilation errors, calling the copy constructor will cause a double free of the pointer on destruction. Commented Nov 7, 2013 at 15:28
  • 1
    @Angew Regarding operator= I'd say that he might provide one taking t and one taking a smart_ptr<t>. They should both return this as a smart_ptr<t>& though. Commented Nov 7, 2013 at 15:43
  • 1
    @user1158692 Given the body of the assignment op the OP posted, they were obviously after the copy assignment op, though. And I think an overload taking t would be weird. Taking a t*, maybe, but just t doesn't feel right. Commented Nov 7, 2013 at 18:43

2 Answers 2

3

One obvious error is that your assignment operator should take and return references to smart_ptr, not t:

  smart_ptr& operator = (const smart_ptr& lhs)
//^^^^^^^^^                    ^^^^^^^^^

Next, your copy constructor calls sm->ptr when it should be calling sm.ptr, since -> has been overloaded to return a t anyway:

smart_ptr(const smart_ptr& sm) : ptr(sm.ptr) { .... }
Sign up to request clarification or add additional context in comments.

Comments

2

Compilation errors could be fixed with the following changes:

    // copy ctor
    smart_ptr(const smart_ptr &sm)
      : ptr(sm.ptr)
    {
       cout<<"copy constructor is called" << endl;
    }

    // destructor's invocation
    p2.~smart_ptr();

However there's a logic error in the copy constructor as the underlying object will be deleted twice (or more times).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.