Code so o.k.?
-
Ist der Code so o.k., insbesondere hinsichtlich class Date?
#include <iostream> #include <conio.h> #include <cstring> using namespace std; class Date { int d_; int m_; int y_; char str_[10]; static Date standardDate; public: Date(int d=0, int m=0, int y=0); Date(const char* str); const char* c_str(); static void setStandard(int d, int m, int y); }; Date::Date(int d, int m, int y) { d_ = d ? d : standardDate.d_; m_ = m ? m : standardDate.m_; y_ = y ? y : standardDate.y_; } Date::Date(const char* str) { // TODO } const char* Date::c_str() { itoa( (d_-(d_%10))/10, str_, 10); itoa( d_%10, str_+1, 10); str_[2] = '-' ; itoa( (m_-(m_%10))/10, str_+3, 10); itoa( m_%10, str_+4, 10); str_[5] = '-' ; itoa( (y_- (y_%10-((y_%100-(y_%1000))/10))/100)/1000, str_+6, 10); itoa( (y_%10-((y_%100-(y_%1000))/10))/100, str_+7, 10); itoa( (y_%100-(y_%1000))/10, str_+8, 10); itoa( y_%1000, str_+9, 10); return str_; } void Date::setStandard(int d, int m, int y) { Date::standardDate = Date(d,m,y); } Date Date::standardDate = Date(1,1,1); int main() { Date::setStandard(19,2,2005); Date t; cout << t.c_str() << endl; getch(); }
-
Nein, zu unleserlich. Da sollten Kommentare rein.
-
Kommentare bei dem Bißchen Code?
-
Wenn du das nicht willst, dann änder wenigstens Date::c_str().
-
noobie schrieb:
Kommentare bei dem Bißchen Code?
Ja, du hast recht. Schreib lieber den Code selbsterklärend
Schau dir mal std::stringstream an und gib dann lieber einen std::string zurück.
str_ ist Verschwendung. Lass es am besten ganz weg, ansonsten aktualisier ihn zumindest nur wenn das Datum sich ändert.
Über die Variablennamen könnte man sich fast streiten, ich zumindest würde day_, month_ und year_ bevorzugen.
Die Auswirkungen des Standarddatums sollten wirklich sehr, sehr gut dokumentiert sein ("Date(3, 0, 1999)").
Generell könnte eine Überprüfung des Datums nicht schaden ("Date(-1, 42, 0)")