Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behavior in constructor

I have a class made up of several fields, and I have several constructors. I also have a constructor that doesn't take any parameters, but when I try to use it:

int main {
    A a;
}

The compiler generates an error, while if I use it like this:

int main {
    A a();
}

It's ok. What's that?

Thank you

like image 800
tunnuz Avatar asked Feb 03 '26 23:02

tunnuz


1 Answers

The first main uses A's default constructor. The second one declares a function that takes no parameters and returns an A by value, which probably isn't what you intend.

So what does the definition of A look like and what is the error that the compiler generates?

Oh, and you need to provide a parameter list in the declaration of main: int main() { //... , not int main { //...

like image 98
CB Bailey Avatar answered Feb 05 '26 13:02

CB Bailey



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!