2

I have a function Func(string str,int*i=NULL,int*j=NULL,bool ok=false); I called it as Func(some_string,false);

And program is crashing..Because the order I'm calling with --is wrong??

5
  • 1
    Yes, you can only omit arguments at the end of the argument list, not in the middle. Commented Dec 3, 2012 at 9:30
  • Post the code, including the call, and a description of what you mean by "crashing". Commented Dec 3, 2012 at 9:31
  • If this actually compiles, it just initialises i with false, which apparently gets somehow converted to a null pointer value. Does the function work of you call it just as Func(some_string)? Commented Dec 3, 2012 at 9:31
  • @Angew: Indeed it does. Any integral constant expression equal to zero will convert to the null pointer. false is integral, constant, and equal to zero, so it meets all criteria. Commented Dec 3, 2012 at 12:00
  • @MSalters I don't know about C++03, but in C++11 [conv.ptr]p1: "A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or ..." (emphasis mine). bool is integral, but it's not an integer type. Commented Dec 3, 2012 at 12:45

2 Answers 2

2

If you wish to specify a value for ok, you also have to specify values for all arguments that precede it.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes. The second parameter is expecting a int value and you are passing a bool value. The 3rd and 4th parameters will take NULL and false respectively.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.