duidae / cpp-practice

practice for c++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

c++ practice

C++

  int& getInt() {
    int i;
    return i;  // DON'T DO THIS.
  }
  
  int& getInt() {
    int* i = new int;
    return *i;  // DON'T DO THIS.
  }
  
  int& getInt()
  {
    static int x = 4;
    return x;  // OK
  }
std::string s0 = "hello";
const char *s1;
s1 = s0.c_str();
class FooC : public FooA, public FooB {  // C繼承A+B
}
enum GameStatus
{
  Menu,      // 開頭選單
  Loading,   // 載入畫面
  Playing,   // 遊戲進行中
  Pause,     // 遊戲暫停
  GameOver   // 遊戲結束
}
 
GameStatus status = GameStatus.Menu;
 
if ( status == GameStatus.Menu )
{
  // 開頭選單處理......
}
if ( status == GameStatus.Playing )
{
  // 遊戲進行中處理......
}

良好的軟體工程基本原則

  • 最小權限原則:利用const於compile time強制執行最小權限原則
  • const: 不能改東西
    • const member
      • const int time;
      • const object不能用assign, 必須用初始化設值
    • const memeber function: function不能修改物件內容, 唯讀
      • int getTime() const;
    • const object: 不能呼叫非const的member function
    • const pointer: 不能修改pointer
      • const char* the_string
      • char* const the_string
      • const char* const the_string
char* the_string : I can change the char to which the_string points, and I can modify the char at which it points.
const char* the_string : I can change the char to which the_string points, but I cannot modify the char at which it points.
char* const the_string : I cannot change the char to which the_string points, but I can modify the char at which it points.
const char* const the_string : I cannot change the char to which the_string points, nor can I modify the char at which it points.

GCC compiler

Makefile

Compile, linking

GCC

ctags

  • $ sudo yum install -y ctags
  • $ ctags -R // R要大寫

IDE

  • VS code
    • "editor.fontSize": 18,
    • "editor.fontWeight": "bold"

About

practice for c++


Languages

Language:C++ 98.1%Language:Makefile 1.9%