Search code examples
c++class-constructors

Default constructor behavior when multiple constructors defined


Are default constructors called automatically when another constructor is defined with parameters or do I have to scope to it with : objectName() like the following:

Class TextFileParse {
public:
    TextFileParse() = default;
    TextFileParse( wstring fileName ) : TextFileParse()
    {
        Load( fileName );
    }
};

What if I define the constructor instead of the default and it initializes and calls other methods, do I have to scope to it then? I am working with codes bases that do both and not sure how each are acting (static .lib, I only have headers to trace into). Thank you


Solution

  • Are default constructors called automatically when another constructor is defined with parameters?

    No, they aren't.

    What if I define the constructor instead of the default and it initializes and calls other methods, do I have to scope to it then?

    Yes, if you want to use a functionality from the another constructor you need to use delegating constructors.

    As a general rule, no constructors automatically call other constructors. You need either use delegating constructors (since C++11) or move the common logic into separate methods which are called from multiple constructors.