Re: [问题] qt语法疑问

楼主: allyusd (梦幻贱狗)   2016-10-24 22:27:49
※ 引述《MOONY135 (谈无欲)》之铭言:
: 开发平台(Platform): (Ex: Win10, Linux, ...)
: vs2015
: 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
: qt 5.7
: 额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
: 问题(Question):
: 目前在写有关http的功能 目前网络上会看到这种写法
: http://imgur.com/qJ2uHi1
: QNetworkReply *reply = manager->post(QNetworkRequest(qurl), multiPart);
: QNetworkReply* reply = manager->post(QNetworkRequest(qurl), multiPart);
: 想请问一下 两种写法有什么差别吗?
: QNetworkReply *reply 我觉得应该是内存位置吧?
写法一:QNetworkReply *replay = ...
写法二:QNetworkReply* replay = ...
我也是研究好久才发现原来是 * 号的位置不同,(应该上色的 XD)
这个部份如推文所述的,跟 Qt 无关,但是又有点关系,(后述)
这算是 C++ Code Style 的一部份,也可以用来发动宗教战争。
因为之前针对这部份有好奇过,分享一下之前收集到的资料:
第一个是 Bjarne Stroustrup 的回答:
http://www.stroustrup.com/bs_faq2.html#whitespace
:两种写法都是对的,对于 C 或 C++ 都是有效且有相同的意义。
C 程式设计师较常强调 int *p, *p 是一个 int。
C++ 程式设计师较常强调 int* p,p 是一个 int 指标。
另一份是 The GNU C++ Library - Coding Style
https://gcc.gnu.org/onlinedocs/libstdc++/manual/source_code_style.html
:直接说用第二种,不要用第一种写法。
Notable areas of divergence from what may be previous local practice
(particularly for GNU C) include:
01. Pointers and references
char* p = "flop";
char& c = *p;
-NOT-
char *p = "flop"; // wrong
char &c = *p; // wrong
Reason: In C++, definitions are mixed with executable code. Here,
p is being initialized, not *p. This is near-universal
practice among C++ programmers; it is normal for C hackers
to switch spontaneously as they gain experience.
接着是 Google C++ Style Guide
http://google.github.io/styleguide/cppguide.html
:随便你们怎么选,但是不要混在一起
宣告指标或参考时,在 * 或 & 前面加上一个空白,或者在在后面加上空白,
不要同时在前面都加上空白
char *x; or char* x;
NOT
char * x;
最后一个是 Qt Creator Coding Rules
https://doc-snapshots.qt.io/qtcreator-extending/coding-style.html
:直接说用第一种,不要用第二种。
原因不明
最后我自己是选择依 Qt Code Style 为主,但是这一条除外,
针对 C++ Code Style 有兴趣的人可以参考一下我的笔记。
(本来只是单纯纪绿,所以蛮乱的,有人愿意一起讨论最好 ^^/
http://jian-ching.blogspot.tw/2016/10/c-code-style-guide.html
作者: MOONY135 (谈无欲)   2016-10-25 00:09:00
感谢大能...明天再来发问延续问题><

Links booklink

Contact Us: admin [ a t ] ucptt.com