c++ - 将 const 字符串传递给函数 c++ 时出现意外行为
问题描述
const string
将a 作为参数传递给函数时,我得到了一个意外的行为。
我有这个功能:
std::string getStatusByTopic(const std::string topic){
if (topic.compare(TOPIC_WP_SM_CMD) == 0)
return state_to_string(STATUSES::WP_HANDLER_STATUS);
}
这还没有完全实现。
我有这个功能:
InsertStatus(getStatusByTopic(TOPIC_WP_SM_CMD),msg);
其中声明为:
void Msp::InsertStatus(const std::string status_topic, const std_msgs::String::ConstPtr& new_status)
我有两个问题:
这些函数已成功编译,即使
getStatusByTopic()
当其定义的返回类型为string时,可能不返回任何内容。这怎么可能?当代码被执行时,场景是并且
getStatusByTopic(TOPIC_WP_SM_CMD)
什么都不返回。没有运行时错误,更重要的是 - TOPIC_WP_SM_CMD作为参数传递给InsertStatus()
(定义为广告“/some/topic/”)。哇,这可能吗?
谢谢。
解决方案
推荐阅读
- python - 如何在使用 python igraph 创建的有向图中向边缘标签添加权重?
- angular - mat-menu 总是出现在页尾
- r - 基于日期范围的循环分配ID,如何提高代码效率?
- excel - 如何格式化电子邮件的正文
- number-systems - 大家好。我正在努力将二进制转换为十进制作为 8 位数字
- c++ - 线程指针向量,push_back 导致崩溃
- ios - 安排每小时重复本地通知的问题
- python - 尝试求解隐式方程时,UnboundLocalError '在赋值前引用'
- flutter - 将 onTap 函数传递给颤振中的另一个小部件
- sas - 我使用什么代码在 SAS 中创建 1-50 内的第一个素数?