做視頻搬運(yùn)哪個(gè)網(wǎng)站最賺錢印度疫情最新消息
? ? ? ? 本文主要探討c++相關(guān)關(guān)鍵字的使用。
char
????????char默認(rèn)是unsigned/signed取決平臺,wchar_t寬字符:用于Unicode編碼(超過一個(gè)字節(jié)),用wcin和wcout輸入輸出,字符串為wstring
????????char8_t(20),char16_t(11起),char32_t(11):指定占用字節(jié)數(shù)且是無符號,字符串類u8string,u16string,u32string(20)
邏輯與位運(yùn)算????????
???????and(&&),or(||),not(!),bitand(&),bitor(|),xor(^)and_eq(&=),or_eq(|=)xor_eq(^=),compl(~),not_eq(!=)
引用(&):
? ????????引用在定義時(shí)初始化(指向?qū)ο?,后面不能指向其他對象,指針可在任何時(shí)候指向其他對象
? ????????引用本質(zhì):int &b = a; <==> int * const b = &a;(指針變量const化)
? ????????引用主要用在函數(shù)傳參和返回值,sizeof(引用)是目標(biāo)變量大小,未規(guī)定引用所占空間大小,編譯器會給分配空間
??
enum? ? ? ?
enum class enmu_type_name:unsigned int{MON = 1, THU, WEN};
enum enmu_type_name{MON = 1, THU, WEN};
????????枚舉類型和值類型可以互相轉(zhuǎn)換,但不能運(yùn)算
inline:
????????定義在類聲明之中的成員函數(shù)將自動地成為內(nèi)聯(lián)函數(shù)
????????類外定義inline函數(shù),類定義和成員函數(shù)在同一頭文件,否則編譯無法進(jìn)行置換
class A
{?public:void Foo(int x, int y) { ... } ? //自動地成為內(nèi)聯(lián)函數(shù),即使沒有inline關(guān)鍵字?
}
nullptr
????????C語言中NULL標(biāo)記野指針((void *)0),C++為其他類型((int *)0 ...)
????????nullptr的本質(zhì)
const class nullptr_t
{public:template<class T> inline operator T*()const {return 0;}template<class C, class T> inline operator T C::*() const {return 0;}private:void operator&() const;
} ? nullptr={};
static_assert
????????C編譯錯(cuò)誤用#error輸出,asser運(yùn)行時(shí)錯(cuò)誤退出
????????C++的static_assert靜態(tài)斷言編譯時(shí)錯(cuò)誤退出,
內(nèi)存對齊
????????擴(kuò)大對齊:__attribute__((aligned(n))),縮小對齊:__attribute__((packed))
????????alignas(n)與__attribute__((aligned(n)))相同,alignof(struct s) <==> 返回未對齊結(jié)構(gòu)體大小或擴(kuò)大機(jī)構(gòu)體所占字節(jié)數(shù)
類型轉(zhuǎn)換
????????typeid:返回變量,表達(dá)式,對象,的類型,返回靜/動態(tài)態(tài)類型
static_cast<type-id>(exdivssion)
????????顯示類型轉(zhuǎn)換:基本類型轉(zhuǎn)換,指針類型轉(zhuǎn)換(空針->目標(biāo)類型空針),函數(shù)類型轉(zhuǎn)換(任意類型函數(shù)->void),父類和子類之間指針和引用轉(zhuǎn)換(上行轉(zhuǎn)換安全,下行轉(zhuǎn)換不安全),不能轉(zhuǎn)換掉exdivssion的const、volitale、__unaligned
dynamic_cast< type-id >(exdivssion)
????????Type-id必須是類指針、類引用或者void *,主用于類層次間上行轉(zhuǎn)換和下行轉(zhuǎn)換,類之間交叉轉(zhuǎn)換
reinterpret_cast <new_type>(expression)
????????reintepret_cast?<==> C的強(qiáng)制類型轉(zhuǎn)換,不進(jìn)行類型檢查
const_cast(const_cast<type_id> (expression))
????????常量向非常量轉(zhuǎn)化,用于添加和移除const或volatile修飾
auto(11)
????????自動推導(dǎo)出變量(對象)類型,定義時(shí)初始化,不能一次定義多個(gè)類型的不同變量
????????decltype:編譯器推導(dǎo)目標(biāo)表達(dá)式類型,不要求初始化
????????auto和decltype:
????????????????auto忽略頂層const,decltype保留const
????????????????auto作為類型占用符,decltype類似于sizeof
????????????????auto推斷出引用(解引用)原有類型,decltype推斷出引用
????????????????auto推斷時(shí)會執(zhí)行,decltype做分析
class
????????class是對struct擴(kuò)展,數(shù)據(jù)(成員變量)和方法(成員函數(shù))的封裝,包含數(shù)據(jù)和方法的訪問權(quán)限(private、protected、public)
????????static靜態(tài)成員和方法屬于class,非靜態(tài)屬于對象
????????this本質(zhì)是指向當(dāng)前對象的指針,未定義對象前可在方法中調(diào)用對象成員
????????virtual修飾class成員函數(shù)為虛函數(shù)(基類中),有接口聲明沒實(shí)體,可在派生類中重寫(override)實(shí)現(xiàn)面向?qū)ο蠖鄳B(tài)性
????????final修飾class的不能被被繼承,final修飾成員的方法子類不能重寫
????????using能讓子類去聲明并訪問父類中private成員
????????operator用于運(yùn)算符重載(重定義運(yùn)算符)
????????friend不屬于class的外部函數(shù)訪問class內(nèi)受保護(hù)的成員變量
????????explicit(顯式)、implicit(隱式)修飾構(gòu)造函數(shù)防止構(gòu)造函數(shù)錯(cuò)誤類型轉(zhuǎn)換
const
????????const限制函數(shù)內(nèi)部對類成員變量修改,mutable可突破const成員函數(shù)限制,可以修改特定成員變量
????????constexpr將變量值賦值給const修飾的變量
constexpr int multiply (int x, int y)
{return x * y;
}
const int val = multiply( 10, 10 ); ? //const int val = 100;
????????export定義模板類或模板函數(shù),在,h文件中聲明,類似extern
????????requires用于模板參數(shù)約束
異常處理
throw(int,double,char,long,short)
????????函數(shù)拋出5類exception,throw()不會拋出異常noexcept替代throw()表示不拋出異常,noexcept(bool)拋出任意異常
????????throw異常若沒有catch會向上層傳遞直到被catch,函數(shù)可用throw列表來標(biāo)識拋出的異常
????????標(biāo)準(zhǔn)庫exception類:bad_typeid,typeid運(yùn)算是多態(tài)指針且不為NULL,會拋出異常
????????bad_cast,dynamic_cast多態(tài)基類對象(或引用)到派生類引用的強(qiáng)制類型轉(zhuǎn)換若不安全會拋出異常
namespace
????????避免命名沖突,提高可讀性和可維護(hù)性,支持模塊化和封裝,提高代碼可靠性和擴(kuò)展性
????????命名空間里面可以包含變量、函數(shù)、類型,不能定義在局部作用域
????????相同名稱命名空間編譯時(shí)會合并,相同名稱命名空間不能存在相同變量、函數(shù)、類型的定義
????????未命名空間(全局變量)直接使用,命名空間(全局變量)使用 :: 域作用限定符或using關(guān)鍵字聲明使用
函數(shù)
????????void func(int i,int j);C中func函數(shù)編譯后符號表中為func,C++編譯后符號表中為_Z3funcii所以c++中函數(shù)參數(shù)列表類型、個(gè)數(shù)可變,還可帶有默認(rèn)參數(shù),c不可行
????????C++調(diào)用C庫(使用C規(guī)則編譯):extern "C"{};
demo1:
? ? ? ? key word測試
目錄結(jié)構(gòu):
代碼示例:?
????????CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.20) #最低版本要求SET(CMAKE_CXX_COMPILER "g++") #設(shè)置g++編譯器PROJECT(KeyWord) #設(shè)置工程名MESSAGE(STATUS "test keyword") #打印消息ADD_EXECUTABLE(pro main.cpp) #生成可執(zhí)行文件
????????run.sh?
#!/bin/bashif [ -f ./Makefile ]
thenmake clean
ficmake .makeecho "---------------------------------"./pro
????????main.cpp?
#include <iostream>using namespace std;int bool_test()
{int num = 2;bool logic = !num;cout << boolalpha << logic << endl;return 0;
}int test_cite()
{struct test{int num;int &cite = num;};struct test s;s.num = 1;int tmp = 10;s.cite = 3;//&s.cite = tmp; //引用在定義時(shí)初始化(指向合法地址),后面不能指向其他地址,指針可在任何時(shí)候指向其他地址cout << "sizeof(s.num) : " << sizeof(s.num) << endl;cout << "sizeof(s.cite) : " << sizeof(s.cite) << endl;cout << "sizeof(s) : " << sizeof(s) << endl;cout << "sizeof(struct test) :" << sizeof(struct test) << endl;cout << "num : " << s.num << endl;cout << "cite : " << s.cite << endl;const int &t = tmp;//t = 20; //引用本質(zhì):int &b = a; <==> int * const b = &a;(指針變量const化)return 0;
}int test_enum()
{enum test {ZERO,ONE,TWO};test num; //c++ ,c: enum test numnum = ZERO;cout << "num :" << num << endl;cout << "enmu list :" << ZERO << " " << ONE << " " << TWO << endl;//ZERO++; ZERO = 2; //不能賦值enum tmp {A = 1,B = 4,C = 5};tmp t = tmp(10);cout << "t :" << t << endl;return 0;
}void func(char *p)
{cout << "char func" << endl;
}void func(int *p)
{cout << "int func" << endl;
}int test_nullptr()
{char *pc = nullptr;int *pi = nullptr;func(pi);func(pc);cout << "c++ NULL :" << NULL <<endl;//c++ : NULL == 0, c: NULL == (void *)0return 0;
}int test_static_assert()
{static_assert(sizeof(void *) == 8,"not support 64bit system ");return 0;
}int mem_align()
{struct s1{char a;short b;int c;};struct alignas(16) s2{char a;short b;int c;};cout << "sizeof(s1) :" << sizeof(struct s1) << endl;cout << "alignof(s2) :" << alignof(struct s1) << endl;cout << "sizeof(s2) :" << sizeof(struct s2) << endl;cout << "sizeof(s2) :" << alignof(struct s2) << endl;return 0;
}int test_typeid()
{char a;unsigned char b;signed char c;int d;long e;float f;double g;short h;cout << "typeid(a).name() :" << typeid(a).name() << endl;cout << "typeid(b).name() :" << typeid(b).name() << endl;cout << "typeid(c).name() :" << typeid(c).name() << endl;cout << "typeid(d).name() :" << typeid(d).name() << endl;cout << "typeid(e).name() :" << typeid(e).name() << endl;cout << "typeid(f).name() :" << typeid(f).name() << endl;cout << "typeid(g).name() :" << typeid(g).name() << endl;cout << "typeid(h).name() :" << typeid(h).name() << endl;cout << "typeid(short).name() :" << typeid(short).name() << endl;return 0;
}int type_convert()
{char a = 1;int b;b = a;b = static_cast<int>(a);cout << "b :" << b << endl;class A {public:virtual void Foo() //虛函數(shù){}};class B : public A{};class C : public A{};A *c1 = new B;B* c3 = dynamic_cast<B*>(c1); //下行轉(zhuǎn)換C* c4 = dynamic_cast<C*>(c1); //橫向轉(zhuǎn)換int *pi;char *pc;pi = reinterpret_cast<int *>(pc);const int n = 10;int *t = const_cast<int *>(&n);*t = 20;cout << "n : " << n << endl;cout << "*t : " << *t << endl;return 0;
}int test_auto()
{int a = 5;auto b = 6;auto c = a;decltype(a) e;decltype(b) f;decltype(c) g;auto h = e;auto i = f;auto j = g;const int num = 1;auto tmp = num;decltype(num) t = 8;tmp = 2;//auto忽略const//t = 9; //decltype可推斷出constchar s = 1;auto &s1 = s;cout << "a type : " << typeid(a).name() << endl;cout << "b type : " << typeid(b).name() << endl;cout << "c type : " << typeid(c).name() << endl;cout << "e type : " << typeid(e).name() << endl;cout << "f type : " << typeid(f).name() << endl;cout << "g type : " << typeid(g).name() << endl;cout << "h type : " << typeid(h).name() << endl;cout << "i type : " << typeid(i).name() << endl;cout << "j type : " << typeid(j).name() << endl;cout << "num type : " << typeid(num).name() << endl;cout << "tmp type : " << typeid(tmp).name() << endl;cout << "t type : " << typeid(t).name() << endl;cout << "s1 type : " << typeid(s1).name() << endl;cout << "decltype(s1) type : " << typeid(decltype(s1)).name() << endl;return 0;
}class A
{public:int i;static int j; void func1();static void func2();
};void A::func1()
{this->j = 30;A::j = 40;cout << "this.i :" << this->i << endl;
}void A::func2()
{A::j = 50;cout << "func2" << endl;
}int A::j;int test_class()
{A::j = 10;A a;a.i = 1;cout << "a.i :" << a.i << endl;cout << "A::j :" << A::j << endl;a.func1();cout << "A::j :" << A::j << endl;A::func2();cout << "A::j :" << A::j << endl;return 0;
}class B
{public:void set_value();private:mutable int value;
};void B::set_value()
{value = 0;value++;cout << "value :" << value << endl;
}int test_mutable()
{B b;b.set_value();return 0;
}constexpr int add_num(int a,int b)
{return a+b;
}int test_constexpr()
{const int num = add_num(10,10);// const int num = 20;cout << "num :" << num << endl;return 0;
}template <typename T>T multiplicaty(T a,T b)
{return a*b;
}int test_template()
{cout << "1*2 :" << multiplicaty(1,2) << endl;cout << "1.1*2.2 :" << multiplicaty(1.1,2.2) << endl;return 0;
}int test_abnormal()
{int m,n;cout << "input dividend : ";cin >> m;cout << "input divisor : ";cin >> n;try{if(n == 0)throw(1);}catch(int e){cout << "divisor can't input 0,repeat input divisor : ";cin >> n;}cout << "m/n : " << m/n << endl;return 0;
}int main()
{cout << "------------------------------------" << endl;bool_test();cout << "------------------------------------" << endl;test_cite();cout << "------------------------------------" << endl;test_enum();cout << "------------------------------------" << endl;test_nullptr();cout << "------------------------------------" << endl;test_static_assert();cout << "------------------------------------" << endl;mem_align();cout << "------------------------------------" << endl;test_typeid();cout << "------------------------------------" << endl;type_convert();cout << "------------------------------------" << endl;test_auto();cout << "------------------------------------" << endl;test_class();cout << "------------------------------------" << endl;test_mutable();cout << "------------------------------------" << endl;test_constexpr();cout << "------------------------------------" << endl;test_template();cout << "------------------------------------" << endl;test_abnormal();cout << "------------------------------------" << endl;return 0;
}
?結(jié)果示例:
demo2:
? ? ? ? namespace測試
目錄結(jié)構(gòu):
代碼示例:?
????????CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.20) #最低版本要求SET(CMAKE_CXX_COMPILER "g++") #設(shè)置g++編譯器PROJECT(namespace) #設(shè)置工程名MESSAGE(STATUS "test namespace") #打印消息ADD_EXECUTABLE(pro main.cpp namespace.cpp) #生成可執(zhí)行文件
????????run.sh?
#!/bin/bashif [ -f ./Makefile ]
thenmake clean
ficmake .makeecho "---------------------------------"./pro
????????namespace.hpp?
#ifndef __NAMESPACE_HPP
#define __NAMESPACE_HPPnamespace
{int num;
}namespace n1
{int num;void printf_num(const int num);
}namespace n2
{int num;namespace n3{int num;void printf_num(const int num);}
}#endif
????????namespace.cpp?
#include <iostream>using namespace std;namespace n1
{int tmp;void printf_num(const int num){cout << "num :" << num << endl;}
}namespace n2
{namespace n3{void printf_num(const int num){cout << "num :" << num << endl;}}
}
????????main.cpp?
#include <iostream>
#include "namespace.hpp"using namespace std;
int main()
{num = 1;n1::num = 2;cout << "num :" << num <<endl;n1::printf_num(n1::num);n2::num = 3;cout << "num :" << n2::num << endl;n2::n3::num = 4;n2::n3::printf_num(n2::n3::num);return 0;
}
結(jié)果示例:
demo3:?
? ? ? ? C++調(diào)用C
目錄結(jié)構(gòu):
代碼示例:
?????????CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.20) #最低版本要求SET(CMAKE_CXX_COMPILER "g++") #設(shè)置g++編譯器PROJECT(CPP_CALL_C) #設(shè)置工程名MESSAGE(STATUS "cpp call c") #打印消息ADD_EXECUTABLE(pro main.cpp clib.c) #生成可執(zhí)行文件
????????run.sh?
#!/bin/bashif [ -f ./Makefile ]
thenmake clean
ficmake .makeecho "---------------------------------"./pro
????????clib.h?
#ifndef __CLIBC_H#define __CLIBC_Hvoid func();#endif
????????clib.c?
#include <stdio.h>
#include "clib.h"void func(){printf("c func\n");return;}
????????main.cpp??
#ifdef __cplusplusextern "C"{#endif#include "clib.h"#ifdef __cplusplus} #endifint main(){func();return 0;}
?結(jié)果示例:
?
demo4:?
? ? ? ? c調(diào)用c++
目錄結(jié)構(gòu):
代碼示例:
????????CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.20) #最低版本要求SET(CMAKE_CXX_COMPILER "g++") #設(shè)置g++編譯器PROJECT(CPP_CALL_C) #設(shè)置工程名MESSAGE(STATUS "cpp call c") #打印消息ADD_EXECUTABLE(pro main.c package_func.cpp cpplib.cpp) #生成可執(zhí)行文件
????????run.sh?
#!/bin/bashif [ -f ./Makefile ]
thenmake clean
ficmake .makeecho "---------------------------------"./pro
????????cpplib.hpp?
#ifndef __LIB_HPP#define __LIB_HPPvoid func();#endif
????????cpplib.cpp??
#include <iostream>
#include "cpplib.hpp"using namespace std;void func()
{cout << "c++ func" << endl;return;
}
????????package_func.hpp?
#ifndef __PACKAGE_FUNC_HPP
#define __PACKAGE_FUNC_HPP#ifdef __cplusplusextern "C"{#endifvoid package_func();#ifdef __cplusplus}#endif
#endif
????????package_func.cpp?
#include "cpplib.hpp"
#include "package_func.hpp"void package_func()
{func();}
r
? ? ? ? main.c?
#include "package_func.hpp"int main(){package_func();return 0;}
結(jié)果示例 :