RS>> TM> if (name=="Cyber Con")
RS>> That doesn't work. You're comparing addresses. Use strcmp.
TM> No. I call an overloaded operator==().
RS> And you expect a complete novice to understand that without
RS> even a comment? I assumed that this would be your response,
RS> but I think you should provide the code for the overloaded
RS> operator if you are going to present such an advanced OO
RS> technique to a novice.
Not really - this is standard C++ style. And one of the things that make it
difficult for C programmers to make the switch to C++. Yes, teaching someone
C++ should tell them what is going on in the background, but if I was to post
working code as a snippet, it shouldn't need that as long as name was
declared "string name" rather than "char* name".
RS>> TM> cout >> "Hi Cyber Con";
RS>> TM> ofstream("file1.fil") << name;
RS>> You have to open the file before you can write to it.
TM> No. The ofstream constructor does that.
RS> Oh, really? I don't doubt it can be done, but is that the
RS> default or does that have to be implemented by you?
Default.
RS>> TM> }
RS>> And close it when you're done
TM> No. The ofstream destructor does that.
RS> Sounds like sloppy programming to me, but then I'm very new
RS> to the C++. I feel that IO cleanup should be done explicitly.
It is done explicitly - by the ofstream's destructor. That's the power of
classes - they automatically allocate/deallocate things for you. The
destructor in C++ is called explicitly by the variable going out of scope (if
it is on the stack) or by deleting it (if it is on the heap). This is
similar to C, except that a full function can be called on each object
created/destroyed. This is in contrast to Java where the finalize method is
not called until the garbage collector feels like it.
RS> In my opinion, files should
RS> always be closed explicitly.
Only if you need to keep the variable around longer than the file. Generally
this is not needed, and you should let the destructor do its job.
Trust me - when you start putting reference-counting objects that point to
hierarchies of class types into containers, you will have a hard time knowing
when to close that file - let the destructor do it for you. You will get
cleaner code, and be less likely to have a memory/resource leak. Been there,
done that.
RS> Here's the errors I get when I try to compile your code.
Note that "string" is standard, but not implemented standardly everywhere.
That is most likely your problem. Try a "more up to date" compiler (gcc?).
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|