Analysis and solution of the ambiguity caused by C++ constructor overloading and default parameters (that is, the problem of constructor ambiguity when overloading)

First of all, you need to understand what the constructor is used for? When creating a class object, the compiler allocates memory space for the object and automatically calls the constructor. The constructor completes the initialization of the data members.

Secondly, everyone must understand and remember that a class can only have one default constructor.
The default constructor in two ways:

  1. Define a parameterless constructor;
  2. Define a constructor with parameter default values.

As long as it is guaranteed that only one of the above two methods appears, it is sufficient to ensure that it does not appear at the same time.
Otherwise, the following error will be thrown: error: call of overloaded class name () is ambiguous

Guess you like

Origin blog.csdn.net/qq_42658739/article/details/105302565