0

有一个类有两个相似的构造函数......

class CX
{
public:
    CX(HEADER header,
              __int64    i64Id); 

    CX(HEADER  header,
              const char* pszName);
    // other methods...
};

我想用空指针调用第二个构造函数。

CX *data1 = new CX(&header, NULL); 
//it is ambiguous call to overloaded function

CX *data2 = new CX(&header, (const char*)NULL); 
// is correct, but against our coding standards...

CX *data3 = new CX(&header, static_cast<const char*>(NULL));
// is correct, but agains our coding standards...

const char* pszName = NULL;
CX *data4 = new CX(&header, pszName);
// this is correct but waste of stack.

有没有更好的方法来选择构造函数?
(在这种情况下,nullptr 将是解决方案,但这是不可能的,因为它应该可以在 VC6 中编译)

4

1 回答 1

0

您可以修改以 NULL 作为默认参数的第二个构造函数,并将第一个参数作为唯一参数传递

CX(HEADER 头, const char* pszName=NULL);

于 2013-08-08T11:54:35.283 回答