Suppose I have a class that I want to make sure my compiler (GCC in this case) doesn’t synthesize any constructors or assignment methods for. I’ve found one way to do this which is to just include a const int member in the class, but this doesn’t rub me to well. Is there an attribute or something which signifies this.
Share
If you define (or only declare) it yourself, then the compiler will not define it for you.
While the declaration is enough to prevent the compiler from generating default constructor, it is necessary to define it if your code requires the default constructor, otherwise you’ll get linker error.
In C++11 (the new ISO Standard), you can disable constructors, copy-constructor, and copy-assignment as:
Now the interesting part
You can also selectively disable constructor(s) for selected types which makes
deletemore interesting. Consider this,Object of this class can be created not only with
intargument, but any type which implicitly converts toint. For example,Now suppose, for whatever reason, I don’t want the users of class
Ato create objects withcharorclass B, which fortunately or unfortunately can implicitly convert toint, then you can disable them as:Now here you go:
Online Demo : http://ideone.com/ZVyK7
The error messages are very clear: