+ Reply to Thread
Results 1 to 2 of 2

Thread: What is non copyable object?

  1. #1

    What is non copyable object?

    What is non copyable object?

    How to implement it in C++?

  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    49
    Copy of an object cannot be made if is non copyable object.

    This is useful when there is no such copy require.

    For example, event logging object can be single object and can be non copyable object.

    Code:
    class CMyNonCopyable
    {
    private:
    	CMyNonCopyable(const CMyNonCopyable&) ;
    	CMyNonCopyable& operator =(const CMyNonCopyable&) ;
    
    public:
    	CMyNonCopyable()
    	{
    	}
    
    	~CMyNonCopyable()
    	{
    	}
    } ;
    
    CMyNonCopyable GetMyNonCopyable()
    {
    	CMyNonCopyable objMyNonCopyable ;
    	return objMyNonCopyable ;//error : cannot access private member declared in class 'CMyNonCopyable'
    }
    
    void GiveMeMyNonCopyable(CMyNonCopyable objMyNonCopyable)
    {
    }
    
    void MyNonCompyableObject()
    {
    	CMyNonCopyable objMyNonCopyable1 ;
    	
    	CMyNonCopyable objMyNonCopyable2 = objMyNonCopyable1 ;//error : cannot access private member declared in class 'CMyNonCopyable'
    	
    	CMyNonCopyable objMyNonCopyable3 ;
    	objMyNonCopyable3 = objMyNonCopyable1 ; //error : cannot access private member declared in class 'CMyNonCopyable'
    
    	GiveMeMyNonCopyable(objMyNonCopyable3) ;//error : cannot access private member declared in class 'CMyNonCopyable'
    }
    As described above you will get compile time error if you try to make copy of an object.

+ 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