2013-10-13 23 views
0

是否可以抑制部分默認的構造函數初始化?我現在的默認構造函數是這樣的:抑制默認構造函數的一部分?

Jd::Jd() { 
    time_t utcTime = time(NULL); 
    struct tm tmLocal; 
    localtime_s(&tmLocal, &utcTime); 

    jd_ = gregorian_to_jd( 
         tmLocal.tm_year + 1900, 
         tmLocal.tm_mon + 1, 
         tmLocal.tm_mday, 
         tmLocal.tm_hour, 
         tmLocal.tm_min, 
         tmLocal.tm_sec 
         ); 
} 

,我使用兩個常量來初始化我JD對象:WTIMEOFDAY和NOTIMEOFDAY。

Jd const NOTIMEOFDAY; 
Jd const WTIMEOFDAY; 

我想NOTIMEOFDAY被初始化爲默認的構造的對象,但只有在gregorian_to_jd()方法,而不是整個事情的年,月,日部分。這可能嗎?

編輯:在JD類

Jd(); 
Jd(jdn_t jdn) : jd_(jdn) { } //Sets the internal datamember to whatever is passed in. 
//Jd(bool includeTime); 

和錯誤即時得到構造函數是:

error C2668: 'calendar::Jd::Jd' : ambiguous call to overloaded function 
could be 'calendar::Jd::Jd(bool) 
or  'calendar::Jd::Jd(calendar::jdn_t) 
+0

聽起來像創建一個構造函數來接受你想傳入的參數會做到這一點,但它有點不清楚你問什麼,因爲WTIMEOFDAY和NOTIMEOFDAY在你的示例代碼中沒有。 –

+0

只需在調用'localtime()'(或將'gregorian_to_jd()'函數調用中的那些參數設置爲零)之後將'tm_hour','tm_min'和'tm_sec'設置爲零。 –

回答

2

添加另一個構造函數的參數:

Jd::Jd(int year, int month, int day, int hour, int min, int sec) 
{ 
    jd_ = gregorian_to_jd(year, month, day, hour, min, sec); 
} 

在創建常數:

const Jd NOTIMEOFDAY(2013, 10, 12, 0, 0, 0); // new constructor called 
const Jd WTIMEOFDAY; // default constructor called 

或者你可以用這個方法:

Jd::Jd(bool includeTime = true) 
{ 
    time_t utcTime = time(NULL); 
    struct tm tmLocal; 
    localtime_s(&tmLocal, &utcTime); 

    jd_ = gregorian_to_jd( 
         tmLocal.tm_year + 1900, 
         tmLocal.tm_mon + 1, 
         tmLocal.tm_mday, 
         includeTime ? tmLocal.tm_hour : 0, 
         includeTime ? tmLocal.tm_min : 0, 
         includeTime ? tmLocal.tm_sec : 0); 
} 

然後初始化常數:

const Jd NOTIMEOFDAY((bool)false); 
const Jd WTIMEOFDAY; 

或者......

// this will allow your double version to work 
Jd::Jd(jdn_t jdn, bool includeTime = true) 
{ 
    // assuming what jdn_t looks like, so you'd have to make some adjustments 
    jd_ = gregorian_to_jd( 
         jdn.tm_year + 1900, 
         jdn.tm_mon + 1, 
         jdn.tm_mday, 
         includeTime ? jdn.tm_hour : 0, 
         includeTime ? jdn.tm_min : 0, 
         includeTime ? jdn.tm_sec : 0); 
} 

或者......

// this should fix the whole issue and is probably the better solution 
class Jd 
{ 
    // other members 
public: 
    explicit Jd(bool includeTime); // prevent implicit conversion 
    explicit Jd(jdn_t jdn); // also prevents implicit conversion 
}; 

Jd::Jd(bool includeTime) // no default parameter 
{ 
    time_t utcTime = time(NULL); 
    struct tm tmLocal; 
    localtime_s(&tmLocal, &utcTime); 

    jd_ = gregorian_to_jd( 
         tmLocal.tm_year + 1900, 
         tmLocal.tm_mon + 1, 
         tmLocal.tm_mday, 
         includeTime ? tmLocal.tm_hour : 0, 
         includeTime ? tmLocal.tm_min : 0, 
         includeTime ? tmLocal.tm_sec : 0); 
} 
+0

問題在於,我想讓NOTIMEOFDAY常量初始化爲當前日期,我不想對其進行硬編碼。 – Delete

+0

然後調用'time(NULL); localtime_s(...);'在你初始化常量並傳入這些值之前的某個時間。或者,您可以將默認參數添加到默認構造函數中... –

+0

這與我具有的另一個構造函數衝突:'Jd(double jdn):jd_(jdn){}' – Delete