Can there be only one constructor in class?

When addressing Character knight{windowwidth, windowheight};
Why doesn’t Character addressed, like knight.Character{windowwidth, windowheight};
What happens if there is more constructor??
Can’t there be no more than one constructor per class or something?

You’re declaring a variable of type Character called knight, so you’re calling the constructor of the Character class on line 15.

To answer the other portion of the question, you are allowed to have multiple constructors defined in a class so long as their signatures are different.

class Foo{
  Foo(); //Valid constructor, default
  Foo(int a); //Also valid, called if you pass an int at variable declaration
  Foo(int b); //Compile error! Constructor signature is the same as above
  Foo(int b, bool c); //100% valid, called if you pass in an int and a bool
}

The reason why Foo(int b) does not work is because the compile can’t tell you wanted Foo(int a) or Foo(int b) so it just throws up its hands and revolts like a worker going on strike for employee mistreatment. The compiler does not care if the arguments are named differently, it only cares for the type(s) used.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms