1、通过引用传递参数,函数内修改参数值后,函数外部自动改变;
2、通过指针传递参数,比引用传参好的点是,可以传nullPtr;
3、Tuple
4、Pair
5、std::array 取值麻烦,array.get<0>(sources);
不晓得这个0参数具体含义,不直观;
6、struct包装多个变量,return {x,y};
即可将x,y的值返回给调用方。
1、类似java \c#中的泛型
2、template<typename T>;
3、template<typename T,int N>
4、调用时,才产生对应版本的代码COPY;
5、不能过渡使用;
#include <iostream>
#include <string>
#define Log(x) std::cout << x << std::endl
int main()
{
Log(5);
Log("Hello");
std::cin.get();
}
\是Enter键的转义
1、它是c++的语法糖
2、int a = 5;
int b = 8;
int& ref = a;
此时 ref = 5;它是a的别名;
3、不能修改ref的引用
ref =b,这是不允许的
但可以使用指针方式修改;
int a=5;
int b =8;
int * ref =&a;
(*ref)= 2;
int* p = &b;
(*p) = 1;
此时,a = 2,b =1;
1、需要开启RTTI,会增加一定的开销
2、
Player* p0 = dynamic_cast<Player*>(actuallyEnemy);
if(p0)
{
...
}
3、转换失败,p0 = null;
4、转换成功,p0= Player的实例;CPP6:
auto[name,age] = CreatePerson();
std::cout << name << std::endl;
#include <iostream>
#include <fstream>
#include <optional>
std::optional<std::string> ReadFileAsString(const std::string& filePath)
{
std::ifstream stream(filePath);
if (stream)
{
std::string result;
// read file
stream.close();
return result;
}
return {};
}
int main()
{
std::optional<std::string> data = ReadFileAsString("data.txt");
std::string value = data.value_or("Not present");
std::cout << value << std::endl;
if (data.has_value())
{
std::cout << "File read sucessfully!\n";
}
else
{
std::cout << "File coun't be opened!\n";
}
std::cin.get();
}
#include <iostream>
#include <variant>
int main()
{
std::variant<std::string, int> data;
std::cout << sizeof(std::string) << "\n";
std::cout << sizeof(int) << "\n";
std::cout << sizeof(data) << "\n";
data = "Brian";
std::cout << std::get<std::string>(data) << std::endl;
if (auto value = std::get_if<std::string>(&data))
{
std::string& v = *value;
std::cout << v << std::endl;
}
else
{
}
data = 2;
std::cout << std::get<int>(data) << std::endl;
std::cin.get();
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有