我需要创建一个以日期和时间格式返回char数组的方法。
class DateTime {
int *_day, *_month, *_year, *_hours, *_minutes;
public:
DateTime(int day = 1, int month = 1, int year = 2000, int hours = 0, int minutes = 0) {
_day= new int(day);
_month= new int(month);
_year= new int(year);
_hours = new int(hours);
_minutes= new int(minutes);
}
}
char * ToCharArray() {
}
在给定的类分配中,我必须使用int指针并在main中分配其值。
我尝试用stringstream将整数转换为char,然后尝试以一种有意义的方式将它们装到数组中,但是对我来说却没有用。
我尝试使用手动将值输入到数组中
temp[0] = '0' + *_date;
但是由于某些日期是两位数,因此很难找出格式中的点和空格。
预期结果将是这样:
DateTime date1(19, 6, 2018, 10, 15);
cout << date1.ToCharArray() << endl;
// outputs: 19.6.2018 10:15
答案 0 :(得分:0)
我想支持你。
首要也是最重要的一点:在现代C ++中,您永远不要(或尽可能少地)使用原始指针进行内存管理。始终使用std :: unique:ptr或std :: shared_ptr。
通过使用原始指针进行动态内存管理而造成内存泄漏的风险过高。出于您的目的,我有一个封闭的类DateTime和受保护的日期/时间成员,我无法想象有任何合理的用例可以完全使用指针。如果是出于学术目的,则可以。否则,根本没有必要。
如果要使用指针和动态内存分配,则必须在类的析构函数中删除新的内存。否则,您将保证内存泄漏。并且不要忘记使用nullptr初始化指针。
现在您的问题:
如果只想将类DateTime输出到任何ostream,则应覆盖类中的<<操作符。您可以在下面的代码中看到示例。
如果要将类的数据放入字符串中,请使用std :: string。您不应该使用char *。
如果您仍然想使用char *,我还添加了一个示例。但是请注意。那是只读的。并且只有有效,如dateTimeString一样在范围内。
因此,再次,请尝试避免使用原始指针。没必要。
在下面的示例中,我在开头放置了2个类,其中包含没有指针的更有意义的方法(类DateTime1)和具有指针的类(类DateTime2)。对于测试,您可以选择using语句中要使用的类。
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
class DateTime1 // Version 1
{
public:
DateTime1() {} // Default. Does nothing. Use standard default values
DateTime1(int d, int m, int y, int h, int min) : day(d), month(m), year(y), hours(h), minutes(min) {}
// Option 1. Overwrite the ostream operator
friend std::ostream& operator<<(std::ostream& os, const DateTime1& dt) { return os << dt.day << '.' << dt.month << '.' << dt.year << ' ' << std::setfill('0') << std::setw(2) << dt.hours << ':' << std::setw(2) << dt.minutes; }
// Option 2. Create a std String
std::string toString() {std::ostringstream oss; oss << day << '.' << month << '.' << year << ' ' << std::setfill('0') << std::setw(2) << hours << ':' << std::setw(2) << minutes; return oss.str();}
protected:
int day{ 1 }; int month{ 1 }; int year{ 1970 }; int hours{ 0 }; int minutes{ 0 };
};
class DateTime2 // Version 2
{
public:
explicit DateTime2(int d, int m, int y, int h, int min) : day(new int(d)), month(new int(m)), year(new int(y)), hours(new int(h)), minutes(new int(min)) {}
// MUST delete dynamically allocated memory
~DateTime2() { delete day; delete month; delete year; delete hours; delete minutes; }
// Option 1. Overwrite the ostream operator
friend std::ostream& operator<<(std::ostream& os, const DateTime2& dt) { return os << *dt.day << '.' << *dt.month << '.' << *dt.year << ' ' << std::setfill('0') << std::setw(2) << *dt.hours << ':' << std::setw(2) << *dt.minutes; }
// Option 2. Create a std String
std::string toString() { std::ostringstream oss; oss << *day << '.' << *month << '.' << *year << ' ' << std::setfill('0') << std::setw(2) << *hours << ':' << std::setw(2) << *minutes; return oss.str(); }
protected:
int* day{ nullptr }; int* month{ nullptr }; int* year{ nullptr }; int* hours{ nullptr }; int* minutes{ nullptr };
};
using DateTime = DateTime2; // Seletc DateTime1 or DateTime2
int main()
{
std::cout << DateTime(19, 6, 2018, 10, 15) << '\n';
// or
DateTime date1(19, 6, 2018, 10, 15); // Call the contructor
std::cout << date1 << '\n';
// or
std::string dateTimeString{ date1.toString() }; // Initialize variable dateTimeString
const char* dateTimeCharP{ dateTimeString.c_str() };
std::cout << dateTimeString << '\n';
std::cout << dateTimeCharP << '\n';
return 0;
}
希望这会有所帮助