C++ Sequential File Access - Part 2 of 3 - Serializing Class Objects

preview_player
Показать описание
C++ ASCII File Access - Part 2 of 3 - Serializing Class Objects

C++ Source and Apps
-------------------------------------------------------------------------------------------------------------------------------------------
Source 1: C++ ASCII File Access - Part 1 of 3 - ASCII, fstream, ofstream, ifstream

Рекомендации по теме
Комментарии
Автор

Another great series, best tutorials for sure.

terratrodder
Автор

Writing objects as strings is not generally called Serialization. In serialization you write the objects to a file as is, without breaking the object into strings first.

satishsinghal
Автор

I know its years late but i think i read the older standard (c++98) made functions public by default, the new ones are private by default so if you get any errors it may be because of that. I dont remember where i read it or if its true but im sure the new standard makes it private.

franknavarrete
Автор

Waiting for those interesting Daytona Beach stories, Charles!

chriswinchester
Автор

Great tutorial!
I have only one question.
Why didn't you insert the functions into the person class as public methods?

BlazPecnikCreations
Автор

You should leave a download of the source file so then we can refer to it when we make mistakes, otherwise this has been very useful, thanks!

TheMintyMate
Автор

This is an excellent tutorial! I enter all the code error free then lost it! Any chance you would download the code, so I don't have to reenter. Thanks

fredgebauer
Автор

if(CG->GetpersonName() == "empty"){
cout << "\n\t Always create a Person before loading\n\n";
}

if( DATAFILE.fail()){
cerr << "Cannot open file. \n";
exit(1); //return 1; take the program to stop, both statement do about the same
} //without crashing

This could be a solution to try to read a file without being created.
Constructor goes like this, and for me is working
Person(string n = "empty", string o = "empty", string l = "empty", string r = "empty"){
this->personName = n;
this->Occupation = o;
this->Location = l;
this->References = r;
cout << "\n\t Building a person instance";}

omahdezavalos
Автор

It's still unclear to me on why he decided to construct the Person object on the heap instead that on the stack. Using raw pointers in such cases brings high risk because of manual memory management. It's just not worth it. Even in cases such as when you need to describe Polymorphism, instead of writing Base* b = new Derived;, it is better to write:

Derived d;
Base& b = d;
b.CallMethod();
Because that way you can avoid the causation of memory leaks.


And also do not forget that in that function, CreatePerson, you have opened Pandora's box, because the multitude of memory leaks that can appear with that global pointer initialization in that function, due to the fact that someone can call that unscoped and unsecured function a huge amount of times.

Let C++ manage it's own stack memory. If absolutely necessary, use smart pointers so you can get rid of the overhang which is manual memory management.

I know this video is demonstrating something trivial, but please, stop writing code like that. I see a lot of programmers at interviews doing those same mistakes over and over again, because they did not learn C++, they learnt a combination of C (90%) and C++(10%).


Edit: grammar.

Kromush