C++ temporary variables in initialization list

后端 未结 2 1223
无人共我
无人共我 2020-12-29 07:28

In C++, is there any way to have something like a temporary variable in an initialization list. I want to initialize two constant members with the same instance of something

2条回答
  •  不思量自难忘°
    2020-12-29 08:00

    In C++11 you could use delegating constructors:

    class Baz{
        const Foo f;
        const Bar b;
        Baz(Paramaters p) : Baz(p, temp(p)) { } // Delegates to a private constructor
                                                // that also accepts a Something
    private:
        Baz(Paramaters p, Something const& temp): f(p,temp), b(p,temp) {
            // Whatever
        }
    };
    

提交回复
热议问题