#define with space

爷,独闯天下 提交于 2019-12-18 19:09:14

问题


Is it possible to write define with spaces such as:

#define replace to replacement here

I want to replace "replace to" with "replacement here".

EDIT:

I want to test private members:

I did write

#define private public

but it didn't work for private slots in Qt

so my intend was to use something like

#define private slots: public slots:

anyway I have found another way to test slots and by the way I'm aware of that this is an ugly hack.


回答1:


no, you can't

#define identifier something

what you define must be an identifier which cannot contain space. Nor can it contain hyphen, begin with a number, etc. you can define only an identifier

what you wrote will work

#define replace to replacement here

but not as you expect. This line defined replace to be substituted with to replacement here

hth.




回答2:


If you are doing unit test, you can compile your file with the following flag

-Dprivate=public

Then in your unit test, you will be able to call every private method of your class.

EDIT:

I've recently remarked that using the -fno-access-control flag on gcc compiler allows you to access private method or member. More info on that topic can be found here: Unit testing with -fno-access-control




回答3:


No, that's not possible. Why not just do this instead:

#define replace_to replacement here



回答4:


You could do...

#define replace replacement
#define to here

Watch out for unintended side effects of the defines. You would probably want to #undef them after they've done their job.




回答5:


probably not. It will understand that the first word after the define is the identifier name and the rest is the "body" of that.




回答6:


It kindof sounds like what you are trying to do would be much better served by a global search and replace.



来源:https://stackoverflow.com/questions/5193734/define-with-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!