Calling another constructor when constructing an object with const members

前端 未结 4 1960
谎友^
谎友^ 2020-12-20 23:20

I have a class with const members, and one constructor which calls another constructor with extra values filled in. Normally I could use a colon initializer for

4条回答
  •  我在风中等你
    2020-12-21 00:17

    How about making a helper function:

    class A
    {
        static int initializor(int b) { int n; myfunc(b, &n); return n; }
    public:
        explicit A(int b_) : b(b_), c(initializor(b_)) { }
        A(int b_, int c_)  : b(b_), c(c_)              { }
    
        // ... as before ...
    };
    

提交回复
热议问题