First thanks for your answer. I was starting to think that nobody will answer. Second sorry for posting in the wrong topic.
Concerning parent role I am aware it is a Qt thing. What I found on the web is pretty much when parent destructor is called it goes though the list of its children and deletes them all. I was hoping to find something more detailed but this will have to do.
Now concerning me shooting myself in the foot.
The example above concerning the weird behavior of copy constructor is not something I have ever done. I am aware that it is dangerous because it will lead to confusion and for almost any practical code it is down right stupid. I am not trying to be difficult just trying to understand what is happening under the hood so to speak.
I don't know if it is legal to type part of the Lipmmans book so I will try to make my own example.
Let say that we have created class Book.
class Book
{
public:
Book(std:string);
private:
std::string sName;
};
//Constructor version one
Book::Book (std::string name) : sName(name)
{
//some code
}
//Constructor version two
Book::Book (std::string name)
{
sName = name;
//somecode
}
class Book
{
public:
Book(std:string);
private:
std::string sName;
};
//Constructor version one
Book::Book (std::string name) : sName(name)
{
//some code
}
//Constructor version two
Book::Book (std::string name)
{
sName = name;
//somecode
}
To copy to clipboard, switch view to plain text mode
In first constructor sName is initialized using std::string copy constructor. The program then enters the constructors body and execute rest of the code. In second constructor sName is initialized using std::string default constructor. Only after program enters the constructor body and change sName value using the std::string assignment operator. I was trying to write about this in my previous post but I failed and I apologize for that.
Wysota, sorry for putting you on the spot like this I mean no disrespect, but you are no doubt much better programmer than me. And things like this are probably trivial to you. Like if exception is thrown during construction of an object that object is only partially constructed and is in invalid state. I only read this on the net but I don't know what to do if this happens.
What I know are two things:
1. Proper memory management is important. Most runtime error occur because of improper memory management.
2. I don't know how to manage memory correctly.
I am currently working on an application which need to dynamically manage list of Job object that I will create. Idea that I have and this is just and idea because I will probably find better ways to implement the final desing is following.
Every job object should be some king of widget that has internal data, buttons for opening the dialogs for changing jobs internal data, tree view to show that internal data after the successful change. All job objects should be in a container in something that should be like a parent layout. Parent layout should have button to add new job, button to delete the last job from the container. Also parent layout should draw only the jobs that exist. When the job is deleted it should be removed from the window. The parent layout for job objects should look something like this and be part of the larger window:
| |
| AddJobbutton RemoveJobButtom |
| |
| |
| ________________ |
| | | EditJobButton11 |
| | TreeView1 | EditJobButton12 |
| |_______________| |
| |
| ________________ |
| | | EditJobButton21 |
| | TreeView2 | EditJobButton22 |
| |_______________| |
|_________________________________________________|
| |
| AddJobbutton RemoveJobButtom |
| |
| |
| ________________ |
| | | EditJobButton11 |
| | TreeView1 | EditJobButton12 |
| |_______________| |
| |
| ________________ |
| | | EditJobButton21 |
| | TreeView2 | EditJobButton22 |
| |_______________| |
|_________________________________________________|
To copy to clipboard, switch view to plain text mode
I don't know how to done this right. First version of my application will have fixed number of jobs because I don't know if I will be able to ensure integrity of my application if some "genius" user starts just adding and deleting jobs. Most probably the container in which I decide to keep my Job objects will deal with this.
if ( container.size() < maxSize) //One of Qt container class haven't decided yet which one is the best for my application
container.add(new Job);
if ( !container.empty()) //Probably don't need to check this
container. delete();
if ( container.size() < maxSize) //One of Qt container class haven't decided yet which one is the best for my application
container.add(new Job);
if ( !container.empty()) //Probably don't need to check this
container. delete();
To copy to clipboard, switch view to plain text mode
If I am boring and annoying I apologies. I am just trying to learn and I am aware that most of my questions are not very interesting for you. And by you here I don't me only Wysota but all of the experience programmers on this forum. You know all this stuff. I, on the other hand, am still learning.
So if you could recommend to me literature concerning proper memory management which is not out dated and hopefully is relevant for Qt development I would appreciate it very much.
Sorry if my post was to long. Thanks in advance.
Bookmarks