初始化C++類變數的簡單指令碼

來源:互聯網
上載者:User

程式員是很懶的。我們都知道,成員變數要初始化,要不就會出現很蛋疼的問題。但是一個有50個變數的類,去一個個寫初始化式很傻,又不能跟c一樣直接memset(POD可以)。

想把初始化這部分自動化,在網上找半天找不到相應的指令碼(如果誰有希望能告訴我沒,謝謝:)),於是只好自己寫了個簡單的,順便分享下:

#!/bin/sh#useage#./variables_initializate.sh path_to_your_header_file [op]#op 1,init the variables in the class initialization function.#otherwise,treat as the default modetype="bool|int|long|float|double|char|[[:alnum:]]+\*"# $2 is empty or not equal 1if [[ -z $2 ]] || [ $2 -ne 1 ]; thencat $1 \| grep -E "^[[:space:]]*($type)[[:space:]]*[a-zA-Z0-9]+;" \| sed "s/^[[:space:]]*bool[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1 = false;/" \| sed "s/^[[:space:]]*int[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1 = 0;/" \| sed "s/^[[:space:]]*long[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1 = 0;/" \| sed "s/^[[:space:]]*float[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1 = 0.0f;/" \| sed "s/^[[:space:]]*double[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1 = 0.0f;/" \| sed "s/^[[:space:]]*char[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1 = 0;/" \| sed "s/^[[:space:]]*[[:alnum:]]\{1,\}[[:space:]]\{0,\}\*[[:space:]]\{0,\}\([[:alnum:]]\{1,\}\);/\1 = NULL;/"elsecat $1 \| grep -E "^[[:space:]]*($type)[[:space:]]*[a-zA-Z0-9]+;" \| sed "s/^[[:space:]]*bool[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1(false),/" \| sed "s/^[[:space:]]*int[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1(0),/" \| sed "s/^[[:space:]]*long[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1(0),/" \| sed "s/^[[:space:]]*float[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1(0.0f),/" \| sed "s/^[[:space:]]*double[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1(0.0f),/" \| sed "s/^[[:space:]]*char[[:space:]]\{1,\}\([[:alnum:]]\{1,\}\);/\1(0),/" \| sed "s/^[[:space:]]*[[:alnum:]]\{1,\}[[:space:]]\{0,\}\*[[:space:]]\{0,\}\([[:alnum:]]\{1,\}\);/\1(NULL),/"fi

用法就是 

./variables_initializate.sh 標頭檔路徑 選擇性參數1

如果加了參數,那麼就產生類建構函式裡頭初始化的形式

如果沒加,那麼預設就是直接初始化的方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.