#用空格定义

发布于 2024-10-20 07:28:00 字数 405 浏览 2 评论 0原文

是否可以用空格编写定义,例如:

#define replace to replacement here

我想将“替换为”替换为“替换此处”。

编辑:

我想测试私有成员:

我确实写过

#define private public

,但它不适用于 Qt 中的私有插槽,

所以我的目的是使用类似的东西

#define private slots: public slots:

,无论如何我已经找到了另一种测试插槽的方法,顺便说一句我知道这一点这是一个丑陋的黑客行为。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傾旎 2024-10-27 07:28:00

不,您不能

#defineidentifier东西

您定义的必须是一个不能包含空格的标识符。它也不能包含连字符、以数字开头等。您可以仅定义一个标识符,

您编写的内容将起作用

#define Replace to replacement here

但不是您期望的那样。此行定义了要替换为 replaceto replacement here

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

涫野音 2024-10-27 07:28:00

您可以...

#define replace replacement
#define to here

注意定义的意外副作用。在它们完成工作后,您可能希望#undef它们。

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.

白昼 2024-10-27 07:28:00

如果您正在进行单元测试,您可以使用以下标志编译您的文件

-Dprivate=public

然后在单元测试中,您将能够调用类的每个私有方法。

编辑:

我最近说过,在 gcc 编译器上使用 -fno-access-control 标志允许您访问私有方法或成员。有关该主题的更多信息可以在此处找到:使用 -fno-access-control 进行单元测试< /a>

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

謌踐踏愛綪 2024-10-27 07:28:00

不,那不可能。为什么不直接这样做:

#define replace_to replacement here

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

#define replace_to replacement here
不忘初心 2024-10-27 07:28:00

可能不会。
它将理解定义后的第一个单词是标识符名称,其余的是其“主体”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文