函数应该返回什么C++
What C++ function should return?
我有以下代码片段:
time_t data1 = time(0)+86400;
struct tm * data_emprestimo = localtime( & data1 );
cout << "Hoje: " << data_emprestimo->tm_mday << '/' << (data_emprestimo->tm_mon + 1) << '/' << (data_emprestimo->tm_year + 1900) << endl;
效果很好。
但是我想知道我应该在函数中返回哪种函数来获取哪些 cout 回声并放入一个变量:struct tm?只是TM?数组?字符串?
我试过这样的事情:
struct tm retornaData(int segundosAdd);
...
...
struct tm retornaData(int segundosAdd){
return data_emprestimo;
}
但它没有用。
我已经用谷歌搜索了很多!
struct tm * data_emprestimo
声明指向结构的指针,因此要将结构本身作为值返回,您需要取消引用指针,return *data_emprestimo;
.
在我看来,您希望编写一个返回日期字符串表示形式的函数。
在 c++ 中,std::string 将是字符串数据的自然返回类型。一种更 C 风格的方法可能会建议你传入 char * 作为您希望打印日期的缓冲区 - 一般来说,std::string 被认为是一种更干净、更健壮的方法。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用