<10>head files
#program once
<11>Debug
通过断点调试,查看内存的值
<12>If else
if就是当条件为真的时候跳转到另外一个地址域中去
int x = 5;
bool comprisonResult = x == 5;
if (comprisonResult) {};
<16>Pointer
char* buffer = new char[8];
memset(buffer, 0, 8);
指针的值为内存地址(整数)
#define LOG(x) std::cout<< x << std::endl
<17>References
注:引用不能被修改
int main() {
int a = 5;
int& ref = a;
}
<20> cpp class
类的基本使用方法
以下为一个Log类
#include<iostream>
class Log {
public:
const int LogLevelError = 0;
const int LogLevelWarning = 1;
const int LogLevelInfo = 2;
private:
int m_LogLevel = LogLevelInfo;
public:
void SetLevel(int level) {
m_LogLevel = level;
}
void Error(const char* message) {
if(m_LogLevel >= LogLevelError)
std::cout << "[ERROR]:" << message << std::endl;
}
void Info(const char* message) {
if (m_LogLevel >= LogLevelWarning)
std::cout << "[INFO]:" << message << std::endl;
}
void Warn(const char* message) {
if (m_LogLevel >= LogLevelInfo)
std::cout << "[WARNING]:" << message << std::endl;
}
};
int main()
{
Log log;
log.SetLevel(log.LogLevelWarning);
log.Warn("hello");
}
<21>static
静态变量的作用域仅为cpp文件,
static int s_Variable = 1;
(1)
类中声明静态变量的使用;
#include <iostream>
class Entity {
public:
static int x;//static
static int y;
static void Print() {
std::cout << x << "," << y << std::endl;
}
};
int Entity::x;
int Entity::y;
int main() {
Entity e;
e.x = 1;
e.y = 2;
Entity::Print();
}
(2)静态变量的作用域
可以是函数或者作用域内,这时这个变量是为"本地",它仅可以被作用域内的变量访问。生命
周期是这个整个程序运行时间。
使用静态方法调用类中的方法
#include<iostream>
class Singleton {
public:
static Singleton& Get() {
static Singleton instance;
return instance;
}
void Function(){}
};
int main() {
Singleton::Get().Function();
}
<24>ENUMS
enum Level{
A,B,C
}
Level level = A;
<25>Constructors and Destuctor
#include<iostream>
class Log {
public:
Log() = delete;// 删除构造函数
Log(int X ,int Y){}//可以有多个构造函数
static void Write(){}//不需要实例化
~Log(){
std::cout << "Destroy Log" << std::endl;
}
};
int main() {
Log::Write();
}
<26>Vitual Function
#include<iostream>
#include<string>
class Entity {
public:
virtual std::string GetName() {
return "zyx";
}
};
class player :public Entity {
private:
std::string m_Name;
public:
player(const std::string& name):m_Name(name){}
std::string GetName() override { return m_Name; }
};
void PrintName(Entity* entity) {
std::cout << entity->GetName() << std::endl;
}
int main() {
Entity* e = new Entity();
PrintName(e);
player* p = new player("12345");
PrintName(p);
}
<29>Interface in c++
vitual void Fact() = 0;
虚函数所在的类无法被实例化,在继承这个基类的类里面实现了这个纯虚函数后,这个类才能被实例化。
<32>String
const char* name 这样的字符串末尾“\0”,这是字符串结束的标志。string是一个类。
#include<iostream>
#include<string>
using namespace std;
void PrintString(const string& string) {
cout << string << endl;
}
int main()
{
//const char* name = "zyx";
string name = "zyx"+string("zxy");
bool contains = name.find("zyx") != string::npos;
}
<33>字符串字面量
#include <iostream>
#include <string>
#include <stdlib.h>
int main() {
const char* name = u8"Cherno";
const wchar_t* name2 = L"Cherno";
const char16_t* name3 = u"Cherno";
const char32_t* name4 = U"Cherno";
std::cout << name << std::endl;
std::cin.get();
}
(2)
using namespace std::string literals;
std::string name0 = "Cherno"s + " hello".
字符串相加(1)
string name = R"zyx"
"zyx"
"zyx";
<34>CONST
const 的使用方法4种
(1)在类中使用
class Entity {
private:
int m_X, m_Y;
mutable int var;
public:
int GetX() const {
var = 2;
return m_X;
}
int GetX() {
return m_X;
}
int Sex(int X) {
m_X = X;
}
};
void PrintEntity(const Entity& e) {
std::cout << e.GetX() << std::endl;
}
(2)对常量使用
指针所指的a不变
指针不能被指向其他变量
两种同时拥有
int* const b = NULL;
int const* c = nullptr;//=const int* a = NULL;
const int* const = NULL;