As per "Inside C++ Object Model" a copy constructor is generated (if not declared by the programmer) by the compiler only when at least any one of the below four conditions are true:
When the class contains a member object of a class for which a copy constructor exists (either explicitly declared by the class designer, as in the case of the previous String class, or synthesized by the compiler, as in the case of class Word)
When the class is derived from a base class for which a copy constructor exists (again, either explicitly declared or synthesized)
When the class declares one or more virtual functions
When the class is derived from an inheritance chain in which one or more base classes are virtual
Which means if I have a class with just constructor then copy constructor will not be provided by the compiler.
Lets take an example:
class test
{
test(){}
};
int main()
{
test obj1; //statement 1
test obj2(obj1); //statement 2
}
Above code works fine. Now the problem comes when I add the following lines in class test:
test(const test& rhs) = delete;
"= delete" ensures that copy constructor is not automatically provided. After adding above line I am getting an error for statement 2 which says Use of deleted function test::test(const test&)
.
My question is: as per "Inside C++ Object Model" I don't need a copy constructor for the above class so when I am explicitly saying not to generate a copy constructor (using delete) why am I getting an error? Since I was expecting that the compiler won't need a copy constructor for the above class.
I am using gcc version 4.6.3.