头文件
#include < iostream > 将文件复制黏贴到当前命令所在行
尖括号用于编译器的包含路径,”“双引号可以做一切
#pragma once
XXX
阻止单个头文件多次包含,并转换为当个翻译单元
#ifndef _LOG_H
#define _LOG_H
XXX
#endif
int main()
函数的入口,默认返回0
过程
cpp -> 翻译单元 -> obj(二进制) -> link -> exe
static
static int Multiply(int a,int b)
{
return a * b;
}
使Multiply函数只被声明在当前翻译单元。
函数分为声明和定义。
引用与指针
int a = 1;
int& ref = a;
int* ptr = &a;
ref为引用,不是变量。
ptr为指针,它也是变量。
struct和class
默认访问类型区别
class默认访问类型private
struct默认访问类型public
单例模式
#include<iostream>
class Singleton
{
private:
static Singleton* singleton ;
public:
static Singleton& Get()
{
return *singleton;
}
void Hello()
{
}
};
Singleton* Singleton::singleton = nullptr;
int main()
{
Singleton::Get().Hello();
std::cin.get();
}
构造函数、析构函数
class Cat
{
private:
float weight, height;
public:
Cat(float w, float h)
{
weight = w;
height = h;
}
~Cat()
{
std::cout << "destory" << std::endl;
}
void print()
{
std::cout << weight << "," << height << std::endl;
}
};
void Function() {
Cat cat(10.5, 20.3);
cat.print();
}
int main()
{
Function();
std::cin.get();
}
继承
class Entity
{
public:
float X, Y;
void Move(float x, float y)
{
X += x;
Y += y;
}
};
class Player : public Entity
{
public:
const char* name;
void eat()
{
std::cout << "eating" << std::endl;
}
};
虚函数
class Entity
{
public:
virtual std::string GetName()
{
return "Entity";
}
};
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("Ch");
PrintName(p);
std::cin.get();
}
纯虚函数
class PrintTable
{
public:
virtual std::string GetClassName() = 0;
};
字符串
const char* name1 = "ZhangSan"; /*一个字节*/
const wchar_t* name2 = L"ZhangSan"; /*windows两个 linux和mac都是四个*/
const char16_t* name3 = u"ZhangSan"; /*两个字节*/
const char32_t* name4 = U"ZhangSan"; /*三个字节*/
const int*、int const*、int* const
const int* a = new int;/*不能修改值,能修改址*/
int const* a = new int;/*不能修改值,能修改址*/
int* const a = new int;/*能修改值,不能修改址*/
const int* const a = new int;/*不能修改值,不能修改址*/
mutable
可修改
class Entity
{
private:
std::string m_name;
mutable int m_count = 0;
public:
const std::string& GetName() const
{
m_count++;
return m_name;
}
};
int main()
{
int x = 8;
auto f = [=]() mutable
{
x++;/*复制值8*/
std::cout << x << std::endl;/*输出9*/
};
f();
std::cout << x << std::endl;/*输出8*/
std::cin.get();
}
成员初始化列表
class Entity
{
private:
std::string m_name;
Example m_example;
public:
Entity()
: m_example(Example())
{
m_name = std::string("ZhangSan");
}
};
成员变量初始化两次
class Entity
{
private:
std::string m_name;
Example m_example;
public:
Entity()
{
m_name = std::string("ZhangSan");
m_example = Example();
}
};
成员变量初始化一次
成员变量不代表不会初始化
隐式转换
class Entity
{
private:
std::string m_name;
int m_age;
public:
Entity(const std::string& name)
: m_name(name),m_age(-1){}
Entity(int age)
: m_name("ZhangSan"), m_age(age) {}
};
void PrintEntity(const Entity& entity)
{
}
int main()
{
PrintEntity(22);
PrintEntity(std::string("ZhangSan"));
Entity a = std::string("ZhangSan");
Entity b = 20;
std::cin.get();
}
explicit
class Entity
{
private:
std::string m_name;
int m_age;
public:
Entity(const std::string& name)
: m_name(name),m_age(-1){}
explicit Entity(int age)
: m_name("ZhangSan"), m_age(age) {}
};
不进行隐式转换
重载运算符
struct Vector2
{
float x, y;
Vector2(float x,float y)
: x(x),y(y){}
Vector2 Add(const Vector2& other) const
{
return Vector2(x + other.x, y + other.y);
}
Vector2 operator+(const Vector2& other) const
{
return Add(other);
}
Vector2 Multiple(const Vector2& other) const
{
return Vector2(x * other.x, y * other.y);
}
Vector2 operator*(const Vector2& other) const
{
return Multiple(other);
}
};
std::ostream& operator<<(std::ostream& stream, const Vector2& other) {
return stream << other.x << "," << other.y ;
}
int main()
{
Vector2 position(4.0f, 4.0f);
Vector2 speed(0.5f, 1.5f);
Vector2 powerup(1.1f, 1.1f);
Vector2 result = position.Add(speed.Multiple(powerup));
Vector2 result2 = position + speed*powerup;
std::cout << result2 << std::endl;
std::cin.get();
}
栈的引用
class Entity
{
public:
Entity()
{
std::cout << "create" << std::endl;
};
~Entity()
{
std::cout << "destory" << std::endl;
};
};
class ScopedPtr
{
public:
ScopedPtr(Entity* ptr)
{
this->ptr = ptr;
};
~ScopedPtr()
{
delete ptr;
};
private:
Entity* ptr;
};
int main()
{
{
ScopedPtr a = new Entity;
}
std::cin.get();
}
智能指针
#include<iostream>
#include<memory>
class Entity
{
public:
Entity()
{
std::cout << "creating" << std::endl;
};
~Entity()
{
std::cout << "destorying" << std::endl;
};
};
int main()
{
{
std::unique_ptr<Entity> entity = std::make_unique<Entity>();
}
std::cin.get();
}
共享指针
int main()
{
{
std::shared_ptr<Entity> entity1;
{
std::shared_ptr<Entity> entity = std::make_shared<Entity>();
entity1 = entity;
}
}
std::cin.get();
}
弱指针
int main()
{
{
std::weak_ptr<Entity> entity1;
{
std::shared_ptr<Entity> entity = std::make_shared<Entity>();
entity1 = entity;
}
}
std::cin.get();
}
拷贝构造函数
#include<iostream>
#include<string>
class String
{
private:
char* m_buffer;
unsigned int m_size;
public:
String(const char* string)
{
m_size = strlen(string);
m_buffer = new char[m_size+1];
memcpy(m_buffer, string, m_size + 1 );
};
String(const String& other)
{
m_size = other.m_size;
m_buffer = new char[m_size + 1];
memcpy(m_buffer, other.m_buffer, m_size + 1);
};
~String()
{
delete[] m_buffer;
std::cout << "destorying" << std::endl;
};
char& operator[](unsigned int index)
{
return m_buffer[index];
};
friend std::ostream& operator<< (std::ostream& stream, const String& string);
};
std::ostream& operator<< (std::ostream& stream, const String& string)
{
return stream << string.m_buffer ;
}
int main()
{
String s = "temp";
String t = s;
t[2] = 'a';
std::cout << s << std::endl;
std::cout << t << std::endl;
std::cin.get();
}
箭头获取偏移量
struct Vector3
{
float x, y, z;
};
int main()
{
int offer = (int)&((Vector3*)0)->y;
std::cout << offer << std::endl;
std::cin.get();
}