At least all of the code in the global main function that uses valis, I suppose.Originally Posted by MK27
At least all of the code in the global main function that uses valis, I suppose.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, laserlight is right. Use her code. Your mistake was double: You used the var valis outside of its scope AND you used it after it had failed to construct! It's all right, however, to have critical code in the constructor. After all, a net connection object is useless if the connection fails. Bubba's objection is not valid: You CAN have such an object as a member element too. It is constructed when the containing class C is, and if the member fails to construct so should C. You must catch an exception from trying to instantiate C.
Like I said, how are you supposed to use valis if construction failed? This would invariably lead to a bug. The pointer solution has the same problem. You can only use valis if construction succeeds, so it is a good thing that the language won't let you use it when it might not have.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
MK27, it looks like you might be falling into the trap that some C to C++ learners fall into, in that they make lots of local exception catchers around just the one line that fails. This is the opposite of the ideal situation.
What you need to do is to catch the exception as late or high up as possible.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Yes, that was the complete program in finished form right there
The intent was primarily to identify the exact error. The runtime reporting (on my system) for an uncaught exception is totally uninformative. It just says "int thrown". Which int? As you can see, there are three possibilities. To identify the error, I need to catch it.
The reason I highlighted "You can only use valis if construction succeeds" is because that's not true. You can't use it even if it succeeds, because it goes out of scope with the try block.
The other situation where I find this irritating is in class definition:
if "SomeObj" requires initialization inside "whatever"s constructor, then it need to be a pointer now too! Why not allow an object to be declared uninitialized? This must be a very common scenario (class members that are objects requiring initialization in the constructor).Code:class whatever { SomeClass SomeObj;
I'd write Bjarne a letter explaining how daft he is, but I imagine he is not so daft and this is just impossible to implement for one reason or another, because everyone must have been aware of it for a long time.
Last edited by MK27; 03-21-2010 at 02:27 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
It is true because you can use it in the try block.Originally Posted by MK27
You would just use the constructor initialisation list to invoke the member variable's appropriate constructor.Originally Posted by MK27
If you really want, you can design the class to use two stage construction, i.e., the constructor does not initialise the object, rather, an initialisation function is provided for the task. But this means that it is possible to have objects with invalid state, and this can be Bad Thing.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That's not inside the constructor, eg, for example: the member object is optional and may throw it's own exception, which you want to catch in the constructor. I just noticed you can do that (use try) in the initialization list, but I'm guessing there are some things you can't do.
In the end I did go with two stage construction (the first stage is not empty, it sets a local "errno" which is examined by the second stage before execution proceeds).
As for the member pointer, here's the actual situation: It's optional based on a parameter. I haven't tried yet because I'm not sure how it should look, but can you put an if/else block in an initialization list? Eg, if the param is NULL, I don't want to initialize the object at all -- but I'm guessing "an uninitialized object" cannot exist in C++, just an initialized pointer? This is not a big deal, just want to make sure I am not giving up too soon or missing something.
Last edited by MK27; 03-21-2010 at 02:49 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
If the member is optional then it makes sense for the member to be a pointer.Originally Posted by MK27
Read GotW #66: Constructor Failures.Originally Posted by MK27
So you are not satisfied with the solution that involves just writing the code in the try block? That is one of the reasons to use exceptions in the first place, i.e., the normal flow of control can be written normally, with exceptions handled further down.Originally Posted by MK27
No, but for this you can use the constructor body.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am right, because I talked about avoiding initialisation, not construction. If you can arrange to use members whose constructors do not initialise them (or initialise them to a known invalid state), then you can do two stage construction.Originally Posted by mustermeister
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Nah -- the whole program is about the valis object, I'm not putting it all into a try block. I suppose a try is not really needed, the program has to fail anyway. Except that it does allow the exception to be identified -- without a try/catch it says nothing beyond the fact that "an exception occurred".
Maybe this is actually an inappropriate use of exceptions -- it is easy enough to use an errno type variable or some some check() function to make sure the initialized object is in good working order (in fact I took all the exceptions out and had the init() func return a value for error checking).
I'm also guessing they are for use in libraries and such so that the onus is put on the user: if you don't do error checking in the form of try/catch, then your program will just fail (rather than providing error reporting which can be ignored or left unchecked).
Ie, a member pointer. So definitely: no such thing as an uninitialized object in C++?No, but for this you can use the constructor body.
Last edited by MK27; 03-21-2010 at 03:28 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
You CAN'T catch an exception from a member object IN the constructor. It seems permitted to do so in the initializer list, but that's terribly bad programming practice. Never do so! You don't WANT to do it, because the construction of the container SHOULD fail when the construction of some member does.
I do not understand: what is the point of your program?Originally Posted by MK27
This goes back to the debate between error codes and exceptions. It is easy enough to check, but a check might be forgotten. On the other hand, carelessness can also lead to a lack of exception safety.Originally Posted by MK27
There are, but the norm is to avoid them.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge