C++11 member initializer list vs in-class initializer?

前端 未结 3 1755
走了就别回头了
走了就别回头了 2020-11-30 19:48

What difference between these ways of initializing object member variables in C++11 ? Is there another way ? which way is better (performance) ?:

class any          


        
3条回答
  •  情歌与酒
    2020-11-30 20:49

    Both examples are equivalent.
    Though only if the type is copyable or movable (check it for yourself) and NRVO is actually done (any halfway decent compiler will do it as a matter of course).

    Though if you had many constructors and constructor-chaining were inappropriate, the first method would allow you not to repeat yourself.

    Also, you can use that method to define aggregates with defaults different from aggregate-initialization for (some) members since C++14.

提交回复
热议问题