Hi all,

I am reading about non-copyable objects from this source:
Link

I am not able to understand how making the "=" operator for the base class as private is preventing the derived class to make copies. We could just have a public "operator =" in the derived class that can still make the derived class copyable right?

Could anyone please explain this?
Thanks!

Hi

Praful
facebook fans | targeted facebook fans | youtube views | SMO services company | search engine optimization | google Ads | Social media services

Technically, yes, you could make the derived class copy-assignable. The intent of the "non-copyable" idiom is to make the class non-copyable for users of the class. As an author of a derived class, you can still make it copyable by making a public copy-constructor and a copy-assignment operator. But you cannot do so by using the copy-constructor or copy-assignment operator of the base class (since they are private and undefined), so it means that you need to write your own code to do the copy (assuming that all data members are accessible, i.e., they are private). So, it still makes sense, because you cannot inadvertently make write code that will copy the non-copyable base-class.

Thanks for the reply mike_2000_17!

The intent of the "non-copyable" idiom is to make the class non-copyable for users of the class. As an author of a derived class, you can still make it copyable by making a public copy-constructor and a copy-assignment operator.

Yes, that was the confusion. I thought the idiom would somehow prevent even the author of the derived class to prevent copies for the derived class. I understand that now.

I also have another question. To make a class non-copyable for the users of a class, couldn't we just have it's copy constructor and "=" private, instead of inheriting from this other class? Something like:

class NoCopy {
   public:
          // some interfaces

   private:
      NoCopy& operator=(const NoCopy&);
      NoCopy (const NoCopy &);
};

Why can't we have just this, instead? I somehow cannot understand why the example in the link shows a base class, and another class derived from it. Could you please explain the intent there?

Thanks!

struct A // non-copyable, moveable
{
    A() { /* ... */ }
    ~A() { /* ... */ }

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

    A( A&& ) { /* ... */ }
    A& operator= ( A&& ) { /* ... */ return *this ; }
};

struct B // copyable, non-moveable
{
    B() { /* ... */ }
    ~B() { /* ... */ }

    B( const B& ) { /* ... */ }
    B& operator= ( const B& ) { /* ... */ return *this ; }

    B( B&& ) = delete ;
    B& operator= ( B&& )  = delete ;
};

struct C // non-copyable, non-moveable
{
    C() = default ;
    ~C() = default ;

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

    C( C&& ) = delete ;
    C& operator= ( C&& )  = delete ;
};

why the example in the link shows a base class, and another class derived from it

Syntactic sugar.

struct non_copyable_non_moveable
{
    non_copyable_non_moveable() = default ;
    ~non_copyable_non_moveable() = default ;

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

    non_copyable_non_moveable( non_copyable_non_moveable&& ) = delete ;
    non_copyable_non_moveable& operator= ( non_copyable_non_moveable&& )  = delete ;
};

struct my_class : private non_copyable_non_moveable
{
    // we do not have to do anything more to make my_class non-copyable and non-moveable
};

thanks vijayan121!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.