Can I call a constructor from another constructor (do constructor chaining) in C++?

前端 未结 15 2339
暖寄归人
暖寄归人 2020-11-22 01:27

As a C# developer I\'m used to running through constructors:

class Test {
    public Test() {
        DoSomething();         


        
15条回答
  •  星月不相逢
    2020-11-22 01:40

    If I understand your question correctly, you're asking if you can call multiple constructors in C++?

    If that's what you're looking for, then no - that is not possible.

    You certainly can have multiple constructors, each with unique argument signatures, and then call the one you want when you instantiate a new object.

    You can even have one constructor with defaulted arguments on the end.

    But you may not have multiple constructors, and then call each of them separately.

提交回复
热议问题