程式員是很懶的。我們都知道,成員變數要初始化,要不就會出現很蛋疼的問題。但是一個有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
如果加了參數,那麼就產生類建構函式裡頭初始化的形式
如果沒加,那麼預設就是直接初始化的方式