How do I make a struct argument optional?

Started by
9 comments, last by Beowulf_ 21 years, 5 months ago
Ok, when passing an argument to a function, like so: void CSURFACE::Rect_Crop(SMALL_RECT &rCrop, const SMALL_RECT &rFrame, SMALL_RECT &rMod) I want to make the last argument (rMod) optional. I think the way you would do this with an integer, or so, would be to say: SomeFunction(int x = 0) Am I right? Is there an ''elegant'' way to do this? Or do I have to force it by defining a "default rectangle", or using pointers? Is there some other way to make an arguement optional other than the one I listed?
Advertisement
Use function overloading and create a function:

void CSURFACE::Rect_Crop(SMALL_RECT &rCrop, const SMALL_RECT &rFrame)

that way if the last parameter is specified it will call the first function, and if it is not specified, it would call this function.

________________________
Grab your sword, and get prepared for the SolidSteel RPG engine, coming soon...(i.e. when it''s done)
______________________________"Man is born free, and everywhere he is in chains" - J.J. Rousseau
Goy... that''s not very elegant. I would have to rewrite most of the code twice, or create a new 3rd function both of them call. Or, no... I guess I could have the one with 3 arguements call the one with 2... but that''s still pretty ugly. I know there''s a better way to do this... there has to be. All I want to do is have 1 block of code that executes if the argument is supplied. Anyone else?
Oh, do you want it totally optional, or do you want a default value to be used when an argument is not specified.

If you want the default case then try:
someFunction(int something = 2 );

as the function prototype.

Then you could call someFunction(4); or someFunction();

In the function definition though, just do:
someFunction(int something)
{
//stuff
}

of course, using a struct, you would possibly need to supply a different constructor and such, but that''s how you pass default parameters.

________________________
Grab your sword, and get prepared for the SolidSteel RPG engine, coming soon...(i.e. when it''s done)
______________________________"Man is born free, and everywhere he is in chains" - J.J. Rousseau
When you have a struct, say:
struct wow {
int x,y;
char *s;
};

You can declare a variable by saying,
wow var={1,50,NULL};

If you''re lucky, that''ll work inside the parameter list, too.
They made me do this:
quote:Original post by Beowulf_
void CSURFACE::Rect_Crop(SMALL_RECT &rCrop, const SMALL_RECT &rFrame, SMALL_RECT &rMod)

I want to make the last argument (rMod) optional.

OK, there''s a few ways of doing that...
quote:
I think the way you would do this with an integer, or so, would be to say:

SomeFunction(int x = 0)

That''s parameter defaulting, which is not exactly the same thing as having an optional argument. However, you can use this to achieve the same effect, but it requires you to have a state of SMALL_RECT which represents null. Then you would only process rMod when its state is not equal to the null state.

If there isn''t any possibility of representing such a state, then you need to use a combination of overloading and forwarding, as has been suggested by others.
Then there''s always passing it as a pointer, instead of a reference.

void CSURFACE::Rect_Crop(SMALL_RECT &rCrop, const SMALL_RECT &rFrame, SMALL_RECT *rMod=NULL)
They made me do this:
Ok, to get down to it, what i REALLY want to do, is to make it completely optional. I just thought the only way to do this was to include a default value. How do you go about making an argument completely optional, and how do you check if it was supplied?
I was going to suggest using ellipses, which I believe would work except I notice that you have "SMALL_RECT &rMod". I don''t think it is possible to have a parameter that you are taking in by reference be optional.

If you don''t have to take it in by references then ellipses will work.
quote:Original post by Beowulf_
Ok, to get down to it, what i REALLY want to do, is to make it completely optional.


It all depends what you mean by ''completely optional''.

- If the function is supposed to fill in a default value ... use a default argument.

- If the function is supposed to behave differently, write an overloaded function. (No, that''s not inelegant.)

- If you want an arbitrary number of NON-CLASS arguments, use a variadic function.

You can combine the first two options as Sabreman pointed out, by making your default a unique ''null'' value representing the absence of arguments.




Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement