Advertisement

Default value for a referenced parameter?

Started by January 20, 2001 04:57 AM
2 comments, last by The_Minister 24 years ago
I apoligise for the enigmatic title This is not for any specific project, I''m just curious how one would create a default value for a parameter passed as a referenced. For example, say I have a function called MyFunction(), defined like so: bool MyFunction (int& r_iNumber_) { r_iNumber_ = iSomeValue; return true; } This function does two things: it sets the input to a value and returns something that in real life would depend on the value of the input. I want the input, r_iNumber_, to be optional. I.E. the user could forgoe the extra functionality if it wasn''t required. The only way I know how to do this is to make the parameter a pointer, have the function part of a class and have it declared as such: class MyClass { public: bool MyFunction(const int* c_piNumber_ = NULL); } Then if a parameter was not given, c_piNumber_ would equal NULL, allowing me to check if a parameter was specified, using something like: if (c_piNumber_) { // number was specified, modify } I want to do the same thing, but with references. How do you set a reference to ''NULL'', as it were, so you can check whether or not it has been set? Or maybe there''s a better way to do this altogether? All help is greatly appreciated. The_Minister 1C3-D3M0N Interactive
[email=mwronen@mweb.co.za" onmouseOver="window.status='Mail The_Minister'; return true" onmouseOut="window.status=' '; return true]The_Minister[/email]1C3-D3M0N Interactive
Why don''t you just overload the function with one that doesn''t take any parameters?
Gee Brain, what we gonna do tonight?
Advertisement
I thought about that, so if that''s the only way to do it then I guess I''ll have to use it. Thanks.

The_Minister
1C3-D3M0N Interactive
[email=mwronen@mweb.co.za" onmouseOver="window.status='Mail The_Minister'; return true" onmouseOut="window.status=' '; return true]The_Minister[/email]1C3-D3M0N Interactive
quote: Original post by The_Minister

..How do you set a reference to ''NULL'', as it were, so you can check whether or not it has been set?



bool
MyFunction (int& r_iNumber_ = *(int*)NULL)
{
if(&r_iNumber_)
{
r_iNumber_ = iSomeValue;
return true;
}
return false;
}

This topic is closed to new replies.

Advertisement