C++ Constant structure member initialization

前端 未结 3 1406
死守一世寂寞
死守一世寂寞 2021-01-11 19:07

I\'ve a constant struct timespec member in my class. How am I supposed to initialize it?

The only crazy idea I got is to derive my own timespec

3条回答
  •  礼貌的吻别
    2021-01-11 19:33

    Use initialization list

    class Foo
    {
        private:
            const timespec bar;
    
        public:
            Foo ( void ) :
                bar(100)
            { 
    
            }
    };
    

    If you want to initialize structure with bracers then use them

    Foo ( void ) : bar({1, 2})
    

提交回复
热议问题