Initialize const member variables

后端 未结 4 1867
逝去的感伤
逝去的感伤 2021-01-01 11:47

I have C++ code that boils down to something like the following:

class Foo{
    bool bar;
    bool baz;
    Foo(const void*);
};
Foo::Foo(const void* ptr){
          


        
4条回答
  •  既然无缘
    2021-01-01 12:05

    You may use delegate constructor in C++11:

    class Foo{
    public:
        Foo(const void* ptr) : Foo(complex_method(ptr)) {}
    
    private:
         Foo(const my_struct* s) : bar(calculate_bar(s)), baz(calculate_baz(s)) {}
    
    private:
        const bool bar;
        const bool baz;
    };
    

提交回复
热议问题