OK so seeing what you have is looking pretty good. Theres one thing I would change about it, and thats get rid of the dependency on QT inside the actual text class itself. You'll want to get in the habit of coding so that your interface code and your backend code (all the stuff that you need to run to make your program a program) are separated. For this project it probably doesn't matter, but its a good habit to be in. For each of those you're really just storing a string, so store the string and then if necessary, create another class that has a Text member and contains all the QT class members.
As for saving stuff to a text file in the constructor I would advise against it. Two reasons, writing to disk is a time consuming operation and you dont need to waste time doing it in your constructor. Two it doesn't need to be there. You can do this at any time while your program is running, no reason to do it immediately. As I suggested in my first post, I'd save them all to one file when the user exits the program.
Some clarifications on the last part of my last post. A hashtable is like an large lookup table, allowing things to be looked up in constant time. This is great if you have a lot of data and don't want to spend a lot of time iterating through an array or linked list looking for it. The serialize method would just be something like this:
- Code: Select all
void Serialize(XML *writer) {
writer->write(text);
writer->write(title);
writer->write(whatever);
}
And have one of these methods on each text class. Now the user has requested to quit the program you can loop through your big list of Text classes and on each one, pass them a writer and say write your data here. They write their data and then return and when you've called it on each one of them, you've got a text file with all the stuff written to it nicely. The advantage of having a Serialize method like this, is the class itself takes care of saving what it needs to save. So you could have 20 different types of text classes, and each one takes care of writing their own data rather than you having to figure out which one needs to save which member.
So from here, i'm thinking i should create a separate method to store the text into a file, and another to retrieve the text from a file. Also, it seems that everyone uses pointers and i'm not sure why/what advantage that provides over passing by reference.
Pointers are essentially passing by reference (in C thats the only way to do so), if you pass by reference the compiler will convert it to a pointer for you anyway. One of the most important things about pointer is for allocating dynamic memory. If you need to persist a text class, your call
- Code: Select all
Text newText(language,title,text,tags,audio);
would create the item on the local stack frame, meaning it would only be accessible to whatever created it and any functions that method calls. To create an object that can be accessed from any method you'd have to do
- Code: Select all
Text *newText = new Text(language, title, text, tags, audio);
Now you have a pointer to the new memory allocated for newText, and whenever you pass that pointer anywhere it is always refering to the correct memory location. Another plus for pointers is if you do something like
- Code: Select all
//parent is just a parent class that contains a Text class
Text newText = parent->textClass;
then the structure textClass will be copied to newText. I was dinged on this when doing some Wabbitemu stuff the other day, I was copying a class when all I really needed what a reference to it, but the reverse is true to. You can use a pointer to refer to the same class in memory, or use a local var to create a copy of it which you can manipulate.
Let me know if any of that makes sense, I feel like I'm rambling now :/