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