Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a default constructor in c++?

So I know that after you write a constructor in a class the default constructor goes away, so you have to start initializing every object. However, is there a way to write a default constructor so that you don't have to do this?

Thanks.

like image 383
Victor Odouard Avatar asked Feb 05 '26 02:02

Victor Odouard


1 Answers

Before C++11

class MyClass
{
public:
    MyClass(int x, int y) {}
    MyClass() {}
};

or in C++11

class MyClass
{
public:
    MyClass(int x, int y) {}
    MyClass() = default;
};

You can write as many constructors as you like, but avoid making your class confusing to use.

like image 81
doctorlove Avatar answered Feb 08 '26 23:02

doctorlove



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!