C ++常量结构成员初始化 [英] C++ Constant structure member initialization

查看:202
本文介绍了C ++常量结构成员初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类中有一个常量 struct timespec 成员。我应该如何初始化它?



我得到的唯一疯狂的想法是派生自己的 timespec 它是一个构造函数。



非常感谢!

  #include< iostream> 

class Foo
{
private:
const timespec bar;

public:
Foo(void):bar(1,1)
{

}
}


int main(){
Foo foo;
return 0;
}




编译完成时出错:source.cpp:在构造函数
'Foo :: Foo()':source.cpp:9:36:error:没有匹配的函数调用
'timespec :: timespec(int,int)'source.cpp: 9:36:note注意:候选项为:
在包含在sched.h:34中的文件中,
来自pthread.h:25,
来自/ usr / lib / gcc / i686- pc-linux-gnu / 4.7.2 /../../../../ include / c ++ / 4.7.2 / i686-pc-linux-gnu / bits / gthr-default.h:41,
从/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/i686-pc-linux-gnu/ bits / gthr.h:150,
来自/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7。 2 / ext / atomicity.h:34,
来自/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/ 4.7.2 / bits / ios_base.h:41,
来自/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/ c ++ / 4.7.2 / ios:43,
来自/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/ 4.7.2 / ostream:40,
来自/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7。 2 / iostream:40,
from source.cpp:1:time.h:120:8:note:timespec :: timespec()time.h:120:8:note:candidate expecteds 0
参数,2提供time.h:120:8:注意:constexpr
timespec :: timespec(const timespec&)time.h:120:8:note:candidate
expects 1 argument, .h:120:8:note:constexpr
timespec :: timespec(timespec&&)time.h:120:8:note:candidate expects
1 argument,2 provided

$在C ++ 11中,你可以在构造函数的初始化器列表中初始化一个聚合成员:



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

在旧版本的语言中,您将需要一个工厂函数:

  Foo ():bar(make_bar()){} 

static timespec make_bar(){timespec bar = {1,1}; return bar;}


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 and give it a constructor.

Thanks much!

#include <iostream>

class Foo
{
    private:
        const timespec bar;

    public:
        Foo ( void ) : bar ( 1 , 1 )
        {

        }
};


int main() {
    Foo foo;    
    return 0;
}

Compilation finished with errors: source.cpp: In constructor 'Foo::Foo()': source.cpp:9:36: error: no matching function for call to 'timespec::timespec(int, int)' source.cpp:9:36: note: candidates are: In file included from sched.h:34:0, from pthread.h:25, from /usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/i686-pc-linux-gnu/bits/gthr-default.h:41, from /usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/i686-pc-linux-gnu/bits/gthr.h:150, from /usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ext/atomicity.h:34, from /usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:41, from /usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43, from /usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40, from /usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40, from source.cpp:1: time.h:120:8: note: timespec::timespec() time.h:120:8: note: candidate expects 0 arguments, 2 provided time.h:120:8: note: constexpr timespec::timespec(const timespec&) time.h:120:8: note: candidate expects 1 argument, 2 provided time.h:120:8: note: constexpr timespec::timespec(timespec&&) time.h:120:8: note: candidate expects 1 argument, 2 provided

解决方案

In C++11, you can initalise an aggregate member in the constructor's initialiser list:

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

In older versions of the language, you would need a factory function:

Foo() : bar(make_bar()) {}

static timespec make_bar() {timespec bar = {1,1}; return bar;}

这篇关于C ++常量结构成员初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆