Shortcut for constructor

前端 未结 1 920
走了就别回头了
走了就别回头了 2021-01-28 08:16

Actually I don\'t know how to define this idioms.

In some code i have red something like:

ClassWithAMessage c = \"This is the message\";
<
相关标签:
1条回答
  • 2021-01-28 09:08
    ClassWithAMessage c = "This is the message";
    

    is copy initialization. A copy constructor must be available for this to work. First, a temporary ClassWithAMessage is constructed using the conversion constructor from "This is the message". The temporary is then used with the copy constructor to construct c. This is subject to copy elision (the temp might not be there).

    ClassWithAMessage c("This is the message");
    

    is direct initialization. The conversion constructor is used directly.

    Not really idioms, just different ways to construct an object.

    0 讨论(0)
提交回复
热议问题