non-copyable

Hi, Need your help.

How inheriting a noncopyable class with a private copy constructor and an assignment operator is going to prohibit the use of copy constructor and assignment operator on the derived class? Please consider the following scenario while replying,

• Default copy constructor and assignment operator are generated by the compiler in the derived class and not added by the programmer
• Copy constructor and assignment operator are defined and declared public in the derived class by the programmer
• Copy constructor and assignment operator are defined and declared private in the derived class by the programmer

Thanks and Regards,
Kiran
• Copy constructor and assignment operator are defined and declared public in the derived class by the programmer
• Copy constructor and assignment operator are defined and declared private in the derived class by the programmer

If you make this changes than derived class already wasn't be noncopyable. So why you need derive form noncopyble if want have possibility to copy objects of derived class?
@QTsik: > So why you need derive form noncopyble if want have possibility to copy objects of derived class?

+1

Hmmm... So QTsik gets reported for making a good post?


> How inheriting a noncopyable class with a private copy constructor and an assignment operator
> is going to prohibit the use of copy constructor and assignment operator on the derived class?

In a class that inherits from noncopyable, if the copy constructor and assignment operator are implicitly declared, they are declared as deleted. (C++98: prohibit the implicit declaration of the copy constructor and assignment operator).

struct A : private noncopyable {};

is syntactic sugar for:
1
2
3
4
5
6
7
8
9
10
11
struct A
{
    A() noexcept = default ;
    ~A() noexcept = default ;

    A( const A& ) = delete ;
    A& operator= ( const A& ) = delete ;

    // note: move constructor implicitly declared as deleted
    // note: move assignment implicitly declared as deleted
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <type_traits>

namespace my_library
{
    namespace noncopyable____detail____
    {
        struct noncopyable
        {
            noncopyable() noexcept = default ;
            ~noncopyable() noexcept = default ;

            noncopyable( const noncopyable& ) = delete ;
            noncopyable& operator= ( const noncopyable& ) = delete ;

            // note: move constructor implicitly declared as deleted
            // note: move assignment implicitly declared as deleted
        };
    }
    using noncopyable = noncopyable____detail____::noncopyable ;
}

struct A : private my_library::noncopyable {};

int main()
{
    std::cout << std::boolalpha
              << std::is_copy_constructible<A>::value << '\n' // false
              << std::is_move_constructible<A>::value << '\n' // false
              << std::is_copy_assignable<A>::value << '\n' // false
              << std::is_move_assignable<A>::value << '\n' ; // false

    A x ;
    // A x1(x) ; // *** error: A( const A& ) is deleted
    // A x2 ( std::move(x) ) ; // *** error: A( A&& ) is deleted

    A y ;
    // y = x ; // *** error: A& operator= ( const A& ) is deleted
    // y = std::move(x) ; // *** error: A& operator= ( A&& ) is deleted
}

http://coliru.stacked-crooked.com/a/aae0e6e08bcdb71d
Last edited on
Topic archived. No new replies allowed.