Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 797657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:50:38+00:00 2026-05-14T22:50:38+00:00

I’m writing a class that holds a pointer to a parent object of the

  • 0

I’m writing a class that holds a pointer to a parent object of the same type (think Qt’s QObject system). Each object has one parent, and the parent should not be destroyed when a child is destroyed (obviously).

class MyClass
{
public:
    MyClass(const MyClass* ptr_parent): parent(parent){};
    ~MyClass(){ delete[] a_children; };
private:
    const MyClass* ptr_parent; // go to MyClass above
    MyClass* a_children; // go to MyClass below
    size_t sz_numChildren; // for iterating over a_children
}

(Excuse my inline coding, it’s only for brevity)

Will destroying the “Master MyClass” take care of all children? No child should be able to kill it’s parent, because I would then have pointers in my main program to destroyed objects, correct?

Why might you ask? I need a way to “iterate” through all subdirectories and find all files on a platform independent level. The creation of this tree will be handled by native API’s, the rest won’t. Is this a good idea to start with?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:50:38+00:00Added an answer on May 14, 2026 at 10:50 pm

    This concept is fine, but there are a number of things wrong with the code you posted (although I gather from your comment about “inline coding” that you just wrote this on the fly rather than copying it out of a compilable program).

    1. You will need to have a_children be an array of MyClass pointers, so it will need to have type MyClass**, and you will need to allocate it an reallocate it appropriately as children are added.

    2. delete[] a_children will not delete the children, it will delete the array that holds the pointers to the children. You must, in the MyClass destructor, iterate over the array, deleting each child pointer, and then delete the array. In fact, it would probably be a better idea to use a std::vector<MyClass*> for a_children instead of MyClass*, so then you don’t have to worry about (1). But even with a vector, you will still need to iterate and delete each child in the destructor.

    3. Your children will need to “register” with their parent object somehow. The way you have written this, there is no way for the child object to tell the parent object that it exists. For this reason you probably will not be able to get away with passing a const parent pointer.

    So, as an example:

    class MyClass {
    
    public:
    
      MyClass(MyClass* parent) 
        : m_parent(parent) 
      { 
        if (m_parent) m_parent->registerChild(this); 
      }
    
      ~MyClass() 
      {
        for (std::vector<MyClass*>::const_iterator i = m_children.begin(); 
             i != m_children.end(); ++i) 
        {
          delete *i;
        }
      }
    
      void registerChild(const MyClass* child) 
      {
        m_children.push_back(child);
      }
    
    private:
    
      MyClass* m_parent;
      std::vector<const MyClass*> m_children;
    };
    

    Note that this does not take into account the possibility that children will be destroyed by any means other than destroying their parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.