![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Cherno C++
Veyron98
这个作者很懒,什么都没留下…
展开
-
40 implicit & explicit
1.能帮助简化很多个人代码 2.explicit关键字必须显式的调用#include <iostream>class Entity{private: std::string m_Name; int m_Age;public: Entity(const std::string& name) :m_Name(name), m_Age(-1){} Entity(const int& age) :m_Name("Veyron"), m_Age(age)原创 2021-04-10 21:58:45 · 115 阅读 · 0 评论 -
39.new 2021-04-09
new就是一个operator,像+-=*/一样(可以编译器右键查看其定义看出)也就是可以overload这个operator并改变它的功能展示new在代码中创建变量:#include <iostream>#include<string>//这里采用这种方法可以重新定义修改关键字using String = std::string;class Entity{private: String m_Name;public: Entity() {.原创 2021-04-09 21:16:38 · 109 阅读 · 0 评论 -
38.栈与堆:实例对象的不同 2021-04-09
实例化类有两种方式:(具体读代码备注吧!)#include <iostream>#include<string>//这里采用这种方法可以重新定义修改关键字using String = std::string;class Entity{private: String m_Name;public: Entity() { m_Name = "void_name"; } Entity(std::string name) :m_Name(name) {} //原创 2021-04-09 20:14:52 · 130 阅读 · 0 评论 -
37.Ternary Operators 2021-04-09
比直接写if 、else要简洁很多 可以写带有判断的赋值语句 并且这个语句因为有高级编译器编译,会消耗更少的内存,不会产生赋值的中间变量#include <iostream>static int s_Level = 8;static int s_Speed = 2;int main(){ /*if (s_Level > 5) s_Speed = 10; else s_Speed = 5;*/ //上面注释的语句等价与下面的语句: s_Speed .原创 2021-04-09 17:36:30 · 132 阅读 · 0 评论 -
36.Member initializer Lists
初始化类成员变量的方法,有两种,首先呢第一种#include <iostream>class Entity{private: std::string m_Name;public: //初始化成员变量的方法 Entity() { m_Name = "Veyron_void"; } Entity(const std::string& name) { m_Name = name; } void getname() { std::cout &原创 2021-04-09 17:01:16 · 115 阅读 · 0 评论 -
35.Mutable
一种用法是上次const中讲过并演示的:在类中const修饰的member function,可以修改被mutable修饰的本不能修改的变量(比如修改打印次数功能) 一种lambdas(一次性的小函数)#include <iostream>int main(){ int x = 8; auto f = [=]() mutable //这里方括号内可以填 & 符号(代表传入x的引用,然后会修改x指向地址的x的值) // 也能填 = 符号(但是要加上mutable 关原创 2021-04-09 12:06:35 · 102 阅读 · 0 评论 -
34.Const(使用初始化参数列表,初始化参数)
Const在基常量中使用#include <iostream>#include<string>int main(){ const int MAX_VALUE = 99; //第一种用法: 在*以前,能修改指针值,不能修改指针指向的值 const int* a = new int; //相当于:int const* a = new int; //这样就能限制修改 a pointer指向的值 //*a = 2; //但是可以修改指针地址 a指向的内存原创 2021-04-08 21:09:18 · 314 阅读 · 0 评论 -
String
#include <iostream>#include<string>//第一个传入的string不会在内存中被复制,直接打印。 第二个会复制void PrintString(const std::string& mystring, std::string mystring2) { mystring2 += '!!!'; std::cout << mystring << std::endl; std::cout << mys.原创 2021-04-08 15:37:06 · 62 阅读 · 0 评论 -
Array
#include <iostream>#include<array>class MyArray{public: //生成一个new关键字的数组,这个数组的地址,会发生起码两个跳转,生命周期有些不同。 int* example = new int[5]; MyArray() { for (int i = 0; i < 5; i++) { example[i] = 11; } }};int main(void){ //初始化类中.原创 2021-04-08 13:38:38 · 293 阅读 · 0 评论 -
pure virtual function
#include <iostream>class Printable //先写一个可以打印,所有类名的interface(即是定义method为virtual function){public: virtual std::string GetClassName() = 0;};//定义一个基类,基类会打印自己的名字class Entity : public Printable{public: virtual std::string Getname() { r.原创 2021-04-08 10:03:38 · 147 阅读 · 0 评论 -
Virtual Function 2021-04-07
虚函数使用```c++#include <iostream>class Entity{public: virtual std::string Getname() { return "Entity"; }};class Player : public Entity{private: std::string _name;public: Player(const std::string& name) : _name(n原创 2021-04-07 21:48:07 · 71 阅读 · 0 评论