【问题标题】:C++ constructor call variationsC++ 构造函数调用变体
【发布时间】:2016-05-30 14:55:57
【问题描述】:

在下面的代码中

template <typename T> struct Maybe {
    T* context;

    Maybe(T *context) : context{context} {} .... }

声明和声明有什么区别

Maybe(T *context) : context{context} {} .... }

Maybe(T *context) : context(context) {} .... }

?

这两个选项都编译和运行没有问题。

【问题讨论】:

  • 在这种情况下(哈哈)它们是一样的。

标签: c++ templates constructor


【解决方案1】:

区别在于前者是direct-list-initialization。后者是direct-initialization。在这种情况下,它们都做同样的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2015-12-18
    • 2021-07-19
    • 2011-05-31
    • 2015-09-16
    • 1970-01-01
    相关资源
    最近更新 更多