0

There is a class with two similar constructors...

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

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

I want to call the second constructor with null pointer.

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.

Is there any better way how to select the constructor?
(nullptr would be the solution in this case, but it's not possible because it should be compilable in VC6)

V-X
  • 2,979
  • 18
  • 28
  • 1
    What does your coding standard permit? `#define NULLCHARPTR ((const char*)0)` maybe? – jrok Aug 08 '13 at 11:48
  • Seems your coding standards are very restrictive. Your last solution is the only other possible solution. If you make pszName's type `const char* const` then the compiler will most likely optimise it out. – dunc123 Aug 08 '13 at 11:51
  • 1
    If you can use c++11, use `nullptr`. – arne Aug 08 '13 at 11:51
  • 1
    `CX data = new CX` is Java stye, not C++. So guess what I think about your coding standards... – Manu343726 Aug 08 '13 at 11:52
  • Oh, that was my mistake. I wasn't copy'n'paste-ing the code but typing directly. The define is the nicest solution. – V-X Aug 08 '13 at 11:58
  • @V-X No it's not. The problem is your coding standard that dissallows `static_cast` where it's perfectly applicable. – jrok Aug 08 '13 at 12:00

1 Answers1

0

you can modify 2nd constructor taking NULL as default argument and call it passing first argument as only argument

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