1

So I have this code :

class A{
public:
    A(){}
    A(int){}
};
int main(){
    A x;//I want this to give me error
    A x(1);//or this to give me error
return 0;
}

How to make the class to be impossible to be constructed? without changing visibility of the constructors;

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Ciomegu
  • 47
  • 5

1 Answers1

5

You could remove the user-defined constructor, and make the default constructor deleted (since C++11).

class A{
public:
    A() = delete;
};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • is there any other way? – Ciomegu Jun 02 '16 at 11:40
  • 1
    @Ciomegu The pre-C++11 way is make constructors private and don't define them. But you said don't want to change visibility... – songyuanyao Jun 02 '16 at 11:42
  • I actually found out, make something virtual then equal to 0;Thank you – Ciomegu Jun 02 '16 at 11:43
  • 1
    @Ciomegu: You're talking about pure virtual functions. This does not apply to class constructors. Pure virtual functions will actually force derived classes to implement the function after all. – AndyG Jun 02 '16 at 11:51