-1
#include <iostream>
using namespace std;

class ClassA
{
  const int a;
  int b, c;
      public:
    ClassA(int x, int y):a(10)
    {
       b = x;
       c = y;
    }
    ClassA():a(10)
    {
    }
    void print()
    {
    cout << a << endl;
    }
};

int main()
{
  ClassA objA(10, 20);
  ClassA objB;
  objB = objA;
  objB.print();
  return 0;
}

compiler doesn't create copy assignment operator in the following cases:

  1. Class has a nonstatic data member of a const type or a reference type.
  2. Class has a nonstatic data member of a type which has an inaccessible copy assignment operator.
  3. Class is derived from a base class with an inaccessible copy assignment operator.

In the above cases i understood the case 1 with above example. but i am not getting case 2 and case 3 so please help me to understand with some example.

johnchen902
  • 9,531
  • 1
  • 27
  • 69
nagaradderKantesh
  • 1,672
  • 4
  • 18
  • 30
  • This is pretty much a duplicate of http://stackoverflow.com/questions/4338073/implementation-supplied-copy-constructor-and-assignment-operator (Look at aschepler's answer there for an example for case 3). – jogojapan May 27 '13 at 07:31

1 Answers1

1

Case 2 would be something like this:

class non_assignable { 
   // note: private
   non_assignable &operator=(non_assignable const &source);
};

class whatever { 
    non_assignable n;
};

Case 3 would be something like:

 class whatever : public non_assignable {};

In either case, a copy assignment operator won't be automatically generated for whatever because it includes a non_assignable member/sub-object, which has an inaccessible (private) copy assignment operator.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111