当前位置:网站首页>The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
2022-07-06 17:38:00 【Wzzzzzzx】
void foo() {
QVariant a = 10;
QVariant b = 20;
int c = add(a, b);
return c;
}
int add(const QVariant &a, const QVariant &b) {
int left = a.value<int>();
int right = b.value<int>();
return a + b;
}
Here's add
function , Although it's ours foo
It will be called internally , But if it's not a performance bottleneck , Better be right add
Check the parameters of .
The reason is that the subsequent maintainers are not as clear about the internal implementation of the function as we are , Some wrong types may be passed in .
This involves a defensive programming problem , We are the function writers , Try to prevent others from introducing a wrong thing , And try to solve this wrong thing .
This is similar to the question whether the smart pointer can take out the raw pointer and pass it into the function as an argument . In some cases , If we have enough knowledge about the internal implementation of functions , Ensure that it does not release the resource indicated by the pointer , We can pass the bare pointer .
One is the writer's perspective , One is the perspective of the caller .
边栏推荐
- C# NanoFramework 点灯和按键 之 ESP32
- CentOS7上Redis安装
- Learn the wisdom of investment Masters
- mysql的合计/统计函数
- TCP connection is more than communicating with TCP protocol
- Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
- Kali2021 installation and basic configuration
- How does wechat prevent withdrawal come true?
- 华为认证云计算HICA
- [ciscn 2021 South China]rsa writeup
猜你喜欢
【MySQL入门】第三话 · MySQL中常见的数据类型
PySpark算子处理空间数据全解析(5): 如何在PySpark里面使用空间运算接口
The problem of "syntax error" when uipath executes insert statement is solved
Start job: operation returned an invalid status code 'badrequst' or 'forbidden‘
C # nanoframework lighting and key esp32
TCP connection is more than communicating with TCP protocol
复盘网鼎杯Re-Signal Writeup
Selenium test of automatic answer runs directly in the browser, just like real users.
Wu Jun's trilogy experience (VII) the essence of Commerce
Solr appears write Lock, solrexception: could not get leader props in the log
随机推荐
Grafana 9 正式发布,更易用,更酷炫了!
C # nanoframework lighting and key esp32
Integrated development management platform
TCP connection is more than communicating with TCP protocol
Precipitated database operation class - version C (SQL Server)
connection reset by peer
C# WinForm中DataGridView单元格显示图片
[VNCTF 2022]ezmath wp
【MMdetection】一文解决安装问题
Detailed explanation of data types of MySQL columns
MySQL报错解决
JVM garbage collector part 2
[mmdetection] solves the installation problem
Wordcloud colormap color set and custom colors
Models used in data warehouse modeling and layered introduction
Yarn: unable to load file d:\programfiles\nodejs\yarn PS1, because running scripts is prohibited on this system
How does wechat prevent withdrawal come true?
Application service configurator (regular, database backup, file backup, remote backup)
应用服务配置器(定时,数据库备份,文件备份,异地备份)
Display picture of DataGridView cell in C WinForm