有一个类有两个相似的构造函数......
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中可编译)
答案 0 :(得分:0)
你可以修改第二个构造函数,将NULL作为默认参数,并将其作为唯一参数传递给第一个参数
CX(HEADER标题, const char * pszName = NULL);