Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Argument in C++ have some special properties?

This is an interview test question not homework. The test has been done.

Which of the following statements about default arguments in C++ are correct ?

A. Default Argument cannot be of a user-defined type.
B. Default Argument can never precede non-default arguments
C. Default Argument cannot be of pointer type.
D. Default Argument exist in global heap not function's stack
E. Default Argument are not considered for generating the function's signature. 

I chose B and E. Are these correct? I am not sure about D, is that correct too?

like image 877
user1002288 Avatar asked Jan 18 '23 07:01

user1002288


2 Answers

B is true. A, C and D are false.

E requires clarification. It depends on what is meant by "generating the function's signature".

As far as the compiler is concerned, the signature is the signature. Default arguments only matter when calling the function. That's where the defaults are substituted in. So the function has the signature it is written with.

If "generating the function's signature" means "what is the C++ signature of the function is", then the signature doesn't care whether an argument is default. But if "generating the function's signature" means "how you call it", then it does care about the defaults.

like image 56
Nicol Bolas Avatar answered Jan 24 '23 22:01

Nicol Bolas


A, C, and D are all definitely false. B is definitely true. I'm not so sure about E, I always forget.

like image 35
Puppy Avatar answered Jan 24 '23 23:01

Puppy