当前位置:网站首页>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 .
边栏推荐
- 自动化运维利器-Ansible-Playbook
- Essai de pénétration du Code à distance - essai du module b
- Redis quick start
- JVM 垃圾回收器之Serial SerialOld ParNew
- PostgreSQL 14.2, 13.6, 12.10, 11.15 and 10.20 releases
- Total / statistics function of MySQL
- EasyRE WriteUp
- Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
- Automatic operation and maintenance sharp weapon ansible Playbook
- 07个人研发的产品及推广-人力资源信息管理系统
猜你喜欢
Serial serialold parnew of JVM garbage collector
About selenium starting Chrome browser flash back
基于LNMP部署flask项目
07个人研发的产品及推广-人力资源信息管理系统
连接局域网MySql
复盘网鼎杯Re-Signal Writeup
02 personal developed products and promotion - SMS platform
【MySQL入门】第一话 · 初入“数据库”大陆
虚拟机启动提示Probing EDD (edd=off to disable)错误
JVM 垃圾回收器之Serial SerialOld ParNew
随机推荐
灵活报表v1.0(简单版)
Flink analysis (II): analysis of backpressure mechanism
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
Application service configurator (regular, database backup, file backup, remote backup)
February database ranking: how long can Oracle remain the first?
Flink analysis (I): basic concept analysis
06 products and promotion developed by individuals - code statistical tools
Concept and basic knowledge of network layering
MySQL error reporting solution
Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
【MMdetection】一文解决安装问题
Essai de pénétration du Code à distance - essai du module b
Selenium test of automatic answer runs directly in the browser, just like real users.
Interpretation of Flink source code (I): Interpretation of streamgraph source code
网络分层概念及基本知识
JVM class loading subsystem
Redis quick start
Shawshank's sense of redemption
02个人研发的产品及推广-短信平台
Xin'an Second Edition: Chapter 25 mobile application security requirements analysis and security protection engineering learning notes