Archived

This topic is now archived and is closed to further replies.

How do I make a struct argument optional?

This topic is 5498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites