0

I am viewing a source code and it has got a singleton class like :

class A {
    private:
     A() {}
     ~A() {}
};

And i see no copy constructor and assignment operator. When i talked about it, i got the response that it would not fail.

But i had read that making copy constructor and assignment operator private or inaccessible is important. But i am unable to generate some breaking test cases.

What test case can create two objects of this class ?

Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
  • `A* x = new A(getA()); A* y = new A(*x); A* z = new A(*y); // who cares if it leaks, we're proving the code broken anyway...` It sounds like the person that gave you the response doesn't know C++. You should buy them a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R. Martinho Fernandes Oct 08 '13 at 10:40
  • @R.MartinhoFernandes that wouldn't work, the destructor is private and those are automatic variables. – Luchian Grigore Oct 08 '13 at 10:43
  • @Luchian good point. Fixed. – R. Martinho Fernandes Oct 08 '13 at 10:44

1 Answers1

3

Assuming you can get a hold of an instance of A, you could copy it with

A* a = new A(instance);

An automatic variable wouldn't work because of the private destructor.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625