在不发送任何内容的情况下获取功能的返回
Get return of a function without sending anything
我有一个功能标头:
double countThis(double counter);
然后,在我的主要中,我这样做:
double test = 10;
countThis(test);
随后出现了函数:
double countThis(double counter) {
double counted = counter;
return counted;
}
在底部,我有一个最后一个功能,在这里我想获得double counted
而无需进行countThis(something)
,我只想从MAIN进行的上一个呼叫中获取返回并获得值10(计数)。
实现这种持久性的一种方法是使用类并定义该类的实例:
struct Counter
{
double counted;
double countThis(double counter)
{
return counted = counter; // assign counter to counted, and return that value.
}
};
在使用点:
int main()
{
Counter c;
c.countThis(10);
// c.counted contains the last value sent to countThis, in this case, 10
}
实例 c
用于 persist 您传递给 countThis
的值。
执行此操作:
double test = 10;
double ret;
ret = countThis(test);
// Now the value returned by countThis is in ret
您可以使用全局变量。(双计数)和函数
double countThis(double counter) {
counted = counter;
return counted;
}
将另一个函数定义为仅返回计数
double count() {
return counted;
}
但是,我建议您创建一个结构并在其上制作getter和setter。
为什么不将值复制到double
的新实例?
示例:
double test = 10;
countThis(test);
double something = test;
相关文章:
- 如何在没有实例的情况下获取非静态方法的类型?
- 如何在不等待检索的情况下获取C++中的内存位置?
- 如何在不为其声明变量的情况下获取和使用常量值的地址?
- 在不使用模板的情况下获取 std::array 引用
- 在确定有实现的情况下获取HTTP 501未实现
- 如何将左值传递给函数,仅在没有模板的情况下获取右值
- 如何在不创建实例的情况下获取类的方法成员的类型?
- 如何在不包含 Windows.h 的情况下获取 IsDebuggerPresent 的声明
- <int> 在不破坏堆栈的情况下获取 C++ 中的堆栈总和
- 如何在不引起实例化的情况下获取功能模板的签名
- 如何在不定义其他 fonction getValue() 的情况下获取私有值
- 如何在不获取所有html的情况下获取QTextEdit的html标签?
- 在不发送任何内容的情况下获取功能的返回
- LDAP函数中是否有任何功能可以在不使用ldap_search的情况下获取用户信息
- 如何在不使用QMediaPlayer的情况下获取Qt中音频和视频文件的持续时间
- 我可以在不声明变量类型的情况下获取输入,或者在 c++ 中为同一变量声明多个类型吗?
- 处理POSIX信号:如何在不使用BackTrace_symbols的情况下获取堆栈跟踪
- 在不知道的情况下获取地图的钥匙
- 在不暂停贪吃蛇游戏的情况下获取输入
- 修复错误:在不更改代码的情况下获取临时地址