0

我有以下代码片段:

 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;
}

但它没有用。

我已经用谷歌搜索了很多!

4

2 回答 2

0
struct tm * data_emprestimo

声明一个指向结构的指针,因此要将结构本身作为值返回,您需要取消对指针的引用,return *data_emprestimo;.

于 2012-12-17T15:01:44.333 回答
0

在我看来,您希望编写一个返回日期字符串表示的函数。

在 c++ 中 std::string 将是字符串数据的自然返回类型。更 C 风格的方法可能建议您传入 char * 作为您希望打印日期的缓冲区 - 通常 std::string 被认为是一种更清洁、更健壮的方法。

于 2012-12-17T15:01:56.673 回答