C 關鍵字及運算子的簡單用法1

2021-10-09 01:14:23 字數 2936 閱讀 6616

1.雙冒號作用域運算子

1.1 ::的用途

1.2 **示例

#include

using

namespace std;

int count=

200;

void

test()

intmain()

2.命名空間的簡單使用

2.1 命名空間的簡單用途

namespace命名空間的用途:解決命名衝突的問題

2.2**示例

以下新建test1.cpp,test1.h,test2.cpp,test2.h,show.cpp

#include

using

namespace std;

#include

"test1.h"

#include

"test2.h"

intmain()

#include

"test1.h"

void test1::

test()

#include

"test2.h"

void test2::

test()

```cpp

//以下兩行test1.h和test2.h都寫

#include using namespace std;

namespace test1

namespace test2

如上可見用namespace定義命名空間的方法

namespace 命名空間名

命名空間下,可以放函式,變數,結構體,類,可以巢狀命名空間

例子

namespace demo

;classa;

namespace demo1

}

警告:命名空間必須定義在全域性作用域下

巢狀命名空間的呼叫一樣的寫法,以demo為例

cout<1.2.1無名,匿名命名空間

namespace

寫無名命名空間,相當寫了 static int m_d,static int m_e,只可以在當前檔案使用,並沒有什麼用

命名空間可以起別名

namespace helloworld

void

test()

3.explicit關鍵字

1.1首先問題匯入一下

#include

using

namespace std;

class

mystring

//過載

//explicit mystring(int a)

mystring

(int a)

char

* mstr;

int msize;

}void

test()

intmain()

3.2explicit的作用

防止隱式型別轉換

5.好用的型別轉換

1)static_cast<>() 靜態型別轉換,編譯的時c++編譯器會做型別檢查;基本型別能轉換 但是不能轉換指標型別

2)若不同型別之間,進行強制型別轉換,用reinterpret_cast<>() 進行重新解釋

3)一般性結論:

c語言中 能隱式型別轉換的,在c++中可用 static_cast<>()進行型別轉換。因c++編譯器在編譯檢查一般都能通過;

c語言中不能隱式型別轉換的,在c++中可以用 reinterpret_cast<>() 進行強行型別 解釋。總結:static_cast<>()和reinterpret_cast<>() 基本上把c語言中的 強制型別轉換給覆蓋

reinterpret_cast<>()很難保證移植性。

4)dynamic_cast<>(),動態型別轉換,安全的基類和子類之間轉換;執行時型別檢查 5)const_cast<>(),去除變數的唯讀屬性

5.2static_cast用法和reinterpret_cast用法

void

main01()

5.3dynamic_cast用法和reinterpret_cast用法

class

animal

;class

dog:

public animal

void

doswim()

};class

cat:

public animal

void

dotree()

};class

book

private

:int price;};

void

objplay

(animal *base)

cat *pcat =

dynamic_cast

>

(base);if

(pcat !=

null)}

void

main02()

const_cast用法

//典型用法 把形參的唯讀屬性去掉

void

opbuf

(const

char

*p)void

main()

asp關鍵字函式運算子

作者blog http blog.csdn.dzh2004 選單標記 選項選單 目錄選單 選單格式 一 選單格式 二 說明式選單 標題選單 直線與 標記 標題 超連結標記 錨 圖形標記 框架標記 定義框架內容 不支援框架宣告 表單標記 script vbscript標記索引 基本運算 數字加法及字串...

運算子過載關鍵字operator

operator關鍵字用來過載內建運算子,使用方法如下 public class operatorcontroller controller overload operator public static operatorcontroller operator operatorcontroller ...

C語言的關鍵字,運算子,識別符號

auto按照自動的方式進行變數的儲存 const定義常量或常引數 extern宣告外部變數或函式 register指定變數的儲存型別是暫存器變數 static指定變數的儲存型別是靜態變數,或指定函式是靜態函式 volatile變數的值可能在程式的外部被改變 char字元型資料 short定義短整型變...