+ Reply to Thread
Results 1 to 2 of 2

Thread: How to avoid client to call copy constructor in C++?

  1. #1

    How to avoid client to call copy constructor in C++?

    I have following code:

    Code:
    public:
    	CStudent()
    	{
    	}
    } ;
    
    void UseStudent()
    {
    	CStudent objStudent1 ;
    
    	//I do not allow client of this class to copy CStudent.
    	//How to do this?
    	CStudent objStudent2(objStudent1) ;
    }
    I do not allow client of this class to copy CStudent.

    How to do this?

  2. #2
    Following can be useful.

    Code:
    class CStudent
    {
    private:
    	CStudent(const CStudent&) ;
    	CStudent& operator =(const CStudent&) ;
    public:
    	CStudent()
    	{
    	}
    } ;
    
    void UseStudent()
    {
    	CStudent objStudent1 ;
    
    	CStudent objStudent2(objStudent1) ;//this is a compile time error
    	CStudent objStudent3 ;
    	objStudent3 = objStudent1 ; //this is a compile time error
    }
    Note:
    Make assignment operator and copy constructor private.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts