C++ 是否抑制默认构造函数的一部分?

C++ 是否抑制默认构造函数的一部分?,c++,C++,是否可以抑制部分默认构造函数初始化?我当前的默认构造函数如下所示: Jd::Jd() { time_t utcTime = time(NULL); struct tm tmLocal; localtime_s( &tmLocal, &utcTime ); jd_ = gregorian_to_jd( tmLocal.tm_year + 1900,

是否可以抑制部分默认构造函数初始化?我当前的默认构造函数如下所示:

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;
我不希望imeofDay被初始化为默认构造的对象,而是只使用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)

添加另一个接受参数的构造函数:

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
const Jd NOTIMEOFDAY((bool)false);
const Jd WTIMEOFDAY;
或者,您可以使用以下方法:

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(2013, 10, 12, 0, 0, 0); // new constructor called
const Jd WTIMEOFDAY; // default constructor called
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);
}
或者

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

听起来,创建一个构造函数来获取您想要传递的参数可以做到这一点,但由于示例代码中没有WTIMEOFDAY和NOTIMEOFDAY,所以您的问题有点不清楚。只需在调用
localtime()
后将
tm_hour
tm_min
tm_sec
设置为零即可(或者只需将
gregorian_to_jd()
函数调用中的那些参数设置为零)。问题是我希望NOTIMEOFDAY常量初始化为当前日期,我不想对其进行硬编码。然后调用
time(NULL);localtime_s(…)
初始化常量并传入这些值之前的某个时间。或者,您可以将默认参数添加到默认构造函数中…这与我拥有的另一个构造函数冲突:
Jd(双jdn):Jd(jdn){
只要您将实际的
double
bool
传递给构造函数,采用
double
的构造函数和采用
bool
的构造函数之间就没有冲突。您还可以使用关键字
explicit
来防止转换构造函数被错误调用h显式关键字我得到了不明确的调用错误,不允许我编译