Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

delete only the child class



  • Hi,

    I have a class named "ContentPanel" and in my programme, this class can be transformed into another children class like "Image", "TextEdit" or "TreeView".
    I want to delete only characteristics of the child class. I tried this but this code delete "ContentPanel" too :

    	if (content->content == tr("Text"))
    		delete dynamic_cast<TextEdit *>(content);
    	else if (content->content == tr("Image"))
    		delete dynamic_cast<Image *>(content);
    	else if (content->content == tr("TreeView"))
    		delete dynamic_cast<TreeView *>(content);
    

    When I called a child class for transformed the class "ContentPanel", I do this :

    Image::Image(QString path, ContentPanel *parent)
    {
    	// Some code
    
    	parent = this;
    }
    

    With that, I transform my class ContentPanel into a child class named "Image". And I want only delete Image, not ContentPanel. How can I do ?



  • Ok thanks :)



  • @bozo6919
    First, it took me a while to figure what you meant by "girl". You might like to edit your question to change all occurrences of girl to child (and the girls to children) for others to understand better.

    Second I don't know what you're really trying to do here.

    Third:

    With that, I transform my class ContentPanel into a girl [child] class named "Image".

    Whatever you're trying to do here, setting parent = this; is not going to do anything/what you think it might do. parent is the name of a parameter to your function. That means it's a local variable, you setting it to something isn't going to have any effect. If you think it's setting Image::parent, it is not.

    Someone else may guess better than I what you want, but I think you just want your ContentPanel to have a QWidget *m_content member variable, you new that to your particular widget class instance, and you delete that to get rid of the widget but keep the ContentPanel otherwise the same. No "child classes" or "transforming" involved.

    There may also be a suitable Qt class to use in place of what you're calling your "ContentPanel", depending on just what you want to do with it.



  • Ok thanks a lot, you give me a lot of information :)
    I'll tried tomorrow to do an other things :)


  • Qt Champions 2019

    @bozo6919 If you just want to delete there is no need to cast, simply

    delete content;
    


  • But if I do this, my class child will be deleted too?


  • Qt Champions 2019

    @bozo6919 What child class do you mean? How familiar are you with C++? If you delete an object in C++ then it is deleted. In Qt if you use parent-child relations then children are deleted as soon as parent is deleted.
    This

    if (content->content == tr("Text"))
    		delete dynamic_cast<TextEdit *>(content);
    	else if (content->content == tr("Image"))
    		delete dynamic_cast<Image *>(content);
    	else if (content->content == tr("TreeView"))
    		delete dynamic_cast<TreeView *>(content);
    

    is doing same as

    delete content;
    

    Or are you talking about polymorphism?

    class A
    {
    public:
        virtual ~A() {...}
    };
    
    class B: public A
    {
    public:
        ~B() {}  
    };
    
    A *object = new B();
    // This delete will call both ~B and ~A
    delete object;


  • They are some things who I'm not very familiar with. (for the moment ^^)

    In your example, how I could do to delete only B? (and save attributes of A)


  • Qt Champions 2019

    @bozo6919 Sorry, what you are saying does not make sense.
    If you delete an object it is deleted, completely. So:

    A *object = new B();
    // Here, object will be deleted completely. You can't delete only part of an object,
    // either you delete the complete object or you don't delete anything.
    // An object either exists or not, it cannot exist partially.
    delete object;
    


  • Ok thanks :)


Log in to reply