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 7191069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:39:03+00:00 2026-05-28T19:39:03+00:00

I am working on an implementation of a binary tree in C++. I am

  • 0

I am working on an implementation of a binary tree in C++. I am fairly new to the language, and while I understand what structs and destructors are, I am not sure about what happens with nested classes/structs.

In my implementation, I have a nested struct for my binary node. Does this struct need its own destructor, or will all of the nodes be deleted when the destructor for the binary tree itself get called. Does the answer change if I change the struct to a class?

Also, I was told that if you ever call “new” in a class, you need to call “delete” in the destructor. If I initialize an array in my initialization list for a class (i.e. class: array({0})), do I need to delete [] array in my destructor?

  • 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-28T19:39:04+00:00Added an answer on May 28, 2026 at 7:39 pm

    Nesting one class (or struct) within another has no effect on the lifetime of the objects. All it does is provide a scope for the class’s name, and access to private members in the enclosing class. So your tree nodes won’t be automatically destroyed just by virtue of being nested in the tree class.

    If you want a tree’s nodes to be destroyed when the tree itself is destroyed, you need to ensure that the tree’s destructor does that. If you’re allocating the nodes manually with new, use delete (or delete[]) in the destructor. Alternatively, you can use something like std::auto_ptr (or std::unique_ptr in C++11), which will do the cleanup in its own destructor so you don’t have to write the delete calls yourself.

    Changing a struct to a class has no effect on its behavior at runtime. The only difference between a struct and a class in C++ is that a struct’s body implicitly begins with public: and a class’s body implicitly begins with private:.

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

Sidebar

Related Questions

I've been working on a Fibonacci Heap implementation in Java for about a week
Been trying to find a working implementation of a WPF listview (or listbox) where
I have a working ChangeTrackingList implementation which does its job just fine, but I'd
I am working on an implementation for RSS feeds for a collaboration platform. Say
I'm working on an implementation of the RubyVM and I've searched all over for
Is there a working JPA 2 implementation for JBoss server? Or maybe there is
The title says it! I know that Jon Skeet was working on an implementation
I'm currently working on a large implementation of Class::DBI for an existing database structure,
I am working on a compiler implementation and wish to check if the end
I'm working with the iPhone OpenGLES implementation and I wish to endlessly scroll a

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.