How To Return Nothing In A Template
Is it possible to return nothing from a function which is defined equally int?
What I endeavour to do is:
I accept a stack class,if you peek information technology checks for the position of last item and if its ok function will transport[peek] you a value, otherwise it will say that "stack is empty!" and return nothing.In the example code, the coder uses go out(-i) to exit the program in example of mistake,returning zilch but completely terminates the programme.What exercise you suggest instead of exiting the programme?
| |
Well, first of all, your form shouldn't do any output all on it'due south ain. Just imagine you lot using that grade in a windows program- do you want to rewrite the class everytime you change the environment (sure, this is probably only a practice for you, but don't first doing this in the start place so you won't fall into bad habits).
No, non really. What yous could do would be either throwing an exception, or set a bool variable of the stack (valid) to fake, and then you could provide a function isValid that returns the value of valid. That manner your program won't explode when the user enters a false value, but yous'll as well hand all of the error handling responsibleness to the user. Some people will probably hate y'all for doing that, but it's better than just randomly crashing the program. Nevertheless, throwing an exception is better.
Just want to inquire because it's bothering me, but did the origional writer have those elipses in? Or did you skip writing the destructor and\or atexit(...) commands? Those items matter with a question like the 1 you posted.
@hanst99; Thanks for the reply,aye this is just a do,Im studying inheritance topic in the volume that I follow.Since I didnt cover exception handling yet,I leave this example until later.
@Computergeek01; I didnt include base grade, it has no-arg constructor,doesnt take destructor and no atexit.That was just an example for inheritance,a base grade and derived one,with derived one checking the bounds and wrapping the base grade.I posted the code partly considering I wanted to find a fashion to handle the error instead of just exiting with exit(-1).
Too thank you to jsmith for quick respond.
There are other options depending on the application. Realizing that this is merely a practice example, I recognize that it is hard to brand blueprint decisions without existent requirements.
The easiest option is to return a "special" value to mean empty. For case, if it only positive numbers make sense to be stored in the stack, you could return -i if the stack is empty. This technique won't piece of work, obviously, if all values within a domain are meaningful.
An choice I have used in situations like this is to change the function prototype to:
bool peek(int& value)
The render value indicates whether the peek was successful. The "peeked" value is returned in the reference argument.
A functional possibility is the Maybe monad.
The trouble with this issue is that you most likely need a temporary in gild to both exam and use the result. (If you are actually smart yous can avoid this, but at that bespeak you are getting too circuitous where simpler ways of doing things, similar doug4 and jsmith suggested, would exercise.)
Here's a simple example:
| |
:-]
[edit] Went dwelling and compiled code ... then fixed it
Last edited on
Topic archived. No new replies allowed.
How To Return Nothing In A Template,
Source: http://www.cplusplus.com/forum/beginner/37857
Posted by: jonesandecone.blogspot.com

0 Response to "How To Return Nothing In A Template"
Post a Comment