简体   繁体   English

我可以在引用成员上声明C ++吗?

[英]C++ can i declare on reference member?

In the h file i declare: 在h文件中,我声明:

static IDataSynchronization& sync_data_;

But in the cpp , I have compilation error for sync_data_ : 但是在cpp中,我有sync_data_的编译错误:

IDataSynchronization& MasterSlaveSynchronize::sync_data_;

Error is : 错误是:

MasterSlaveSynchronize::sync_data_ declared as reference but not initialized

I can , but dont want to declare is as a pointer and i want to learn what is the problem here and if there is a way to bypass this. 我可以,但是不想声明是一个指针,我想了解这里的问题是什么,是否有办法绕过它。

I cannot initialize at declaration , because this is kind of callback that will pass at setup (no constructor , static setup). 我不能在声明时初始化,因为这是一种将在安装程序(没有构造函数,静态安装程序)通过的回调。

Thank you 谢谢

可以,但是需要初始化它:

IDataSynchronization& MasterSlaveSynchronize::sync_data_ = something;

References must be initialized in the declaration. 引用必须在声明中初始化。 If as it seems from your comments you cannot do that, then you will have to resort to pointers. 如果从您的评论看来,您不能执行此操作,那么您将不得不求助于指针。 There is no third option there. 那里没有第三种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM