简体   繁体   中英

C++ can i declare on reference member?

In the h file i declare:

static IDataSynchronization& sync_data_;

But in the cpp , I have compilation error for 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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