dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Making a class non-copyable

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,496

    Making a class non-copyable

    The main project I work on uses boost::noncopyable whenever it needs a class to be non-copyable.

    But for projects which don't use libboost I've a vague recollection there was some technique for making a class non-copyable. It was something like making the copy c'tor and assignment operator private (or something along those lines). But I can't remember precisely what was involved or if there was something more to it than that. Can some kind soul remind me..?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: Making a class non-copyable

    It was something like making the copy c'tor and assignment operator private
    Yes, that was the old c++98 way. The modern way is simply to mark the copy c'ctor and copy assignment as =delete.

    Code:
    MyClass(const MyClass&) = delete;
    MyClass& operator=(const MyClass&) = delete;
    If you want move capability, then you'll need to provide the necessary move c'str and move assignment (or use the default if appropriate). eg

    Code:
    MyClass(MyClass&&) = default;
    MyClass& operator=(MyClass&&) = default;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: Making a class non-copyable

    Code:
    private:
        MyClass& operator= (const MyClass&);
    That is the c++98 way to do it. You don't provide a body for the function - just the declaration.

    For more info see https://stackoverflow.com/questions/...t-non-copyable
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: Making a class non-copyable

    Code:
    class nocpy
    {
    public:
    	nocpy() {};
    
    private:
    	nocpy(const nocpy&);
    	nocpy& operator=(const nocpy&);
    };
    
    // ...
    
    	nocpy c1;
    	nocpy c2;
    
    	c2 = c1;
    Gives the compile error " error C2248: 'nocpy:: operator =': cannot access private member declared in class 'nocpy'" as expected.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,805

    Re: Making a class non-copyable

    How does boost::noncopyable do it? Do you have the boost sources to take a look?

  6. #6
    Join Date
    Feb 2017
    Posts
    360

    Re: Making a class non-copyable

    I've thought about having my own noncopyable for quite some time and finally got around to it (by following Arjay's advice above). I seldom need it but once in a while I do and then I always miss the Boost way of doing it conveniently by inheritance. If anyone spots something fishy about it please let me know.

    Code:
    // MODIFIED FROM:
    
    //  Boost noncopyable.hpp header file  --------------------------------------//
    
    //  (C) Copyright Beman Dawes 1999-2003. Distributed under the Boost
    //  Software License, Version 1.0. (See accompanying file
    //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    
    //  See http://www.boost.org/libs/utility for documentation.
    
    #ifndef NOCOPY_ONCE
    #define NOCOPY_ONCE
    
    namnespace maux {
    
    	class Nocopy {	// if (privately) inherited by a class the class becomes non-copyable
    	protected:
    		Nocopy() = default;
    		~Nocopy() = default;
    
    		Nocopy(const Nocopy&) = delete;
    		Nocopy& operator=(const Nocopy&) = delete;
    	};
    }
    
    #endif
    Last edited by wolle; October 23rd, 2018 at 11:25 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)