C++ constructor
It sounds simple enough when trying to open a file or to test the existence of a file.
Code:
{ |
That shouldn't compile. f isn't something that can be implicitly converted to a boolean. To be a NULL object it would have to be a pointer of sometype (in modern c++ a nullptr). To test the f object you can use the state functions (i.e. good, bad, fail). A constructor can't return a null object.
|
There is nothing wrong with writing C in a C++ program:
Code:
FILE *fp; |
It does compile. I used this technique to do quick checks for flag file all the time.
After some "stepping into", I see 'f' returned is a valid object when "filename" does not exist. That leaves me wonder why the statement "if (f) {}" will work. My closest attempt is this: Code:
#include <iostream> |
Quote:
Code:
MyClass c; |
Quote:
IMO, this is much more convoluted than the C method. ETA: My comment assumed "c" was declared as a pointer to MyClass and allocated with new. Ed |
No it is not solved.
Code:
std::ifstream f("filename"); |
Well, some where I missed this. fstream overloads the !operator so that !f is a synonym for fail https://en.cppreference.com/w/cpp/io..._ios/operator!
|
I think that was already explained.
std::ifstream f(<anything>) is a constructor which will create a c++ object named f. Probably usable, but probably useless. Anyway, the class is created. if (anything) is interpreted quite easily: anything is converted to a number (if that was not a number already). f is a non-null object, therefore it will be converted to a non-zero number. Finally if will check if that number (result) was non-zero (in your case it is most probably the pointer to the class instance). http://www.cplusplus.com/reference/f...tream/is_open/ |
"bool" is being overloaded. The function call is implied. http://www.cplusplus.com/reference/i.../operator_bool
IMO, overloading is a terrible feature. A reader can't look at code and figure out what it does without also looking at a bunch of declarations. It is much better to explicitly call the appropriate function. Ed |
Actually, it's not a pointer , it's a class object and it can't be converted to a number but since the !operator (and bool operator) is overloaded you can test it with a bool operation.
i.e. Code:
class A { |
Then it's no magic. Just operator bool().
Code:
{ |
After a quick trip to http://www.cplusplus.com/reference/f...ream/ifstream/
Code:
#include <iostream> |
Quote:
There is absolutely nothing wrong with the way you're checking for the file's existence. Obviously, if you also want to handle the file not existing, you'd extend the code to: Code:
{ And as for this: Code:
how to write the constructor that returns that "null" object? Quote:
Obviously, it is impossible for the constructor to ever return nullptr. That's not how constructors work. You said "null object", and that actually is a pattern that you can use to solve this. https://en.wikipedia.org/wiki/Null_object_pattern#C++ Another option is to have a state variable on the class that determines whether or not the file exists. That's the pattern for Qt code, which does not use exception handling. If you want the correct suggestion, then it actually is "throw an exception". https://isocpp.org/wiki/faq/exceptions#ctors-can-throw |
(It is hard to follow 'the standard C++ ways' as C++ is an union of many different concepts. That's why some contructors throw exceptions, others create a 'marked as broken' object.
IMHO Yossi Kreinin's C++ FQA is a mandatory reading for a beginner.) |
Of course, another option is to overload the ! operator like ifstream does. I'd probably take that option for consistency.
|
All times are GMT -5. The time now is 03:47 PM. |