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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:58:59+00:00 2026-05-28T00:58:59+00:00

Suppose I have a class similar to the following: #include <vector> class element {

  • 0

Suppose I have a class similar to the following:

#include <vector>

class element
{
public:
   element();
   ~element();

   virtual void my_func();

private:
   std::vector<element*> _elements;
};

How would I go about implementing the destructor?

I was thinking something like this, but I’m not sure. I am worried about memory leaks since I’m relatively new to C++.

void element::destroy()
{
   for(int i = 0; i < _elements.size(); ++i)
      _elements.at(i)->destroy();

   if(this != NULL) 
      delete this;
}

element::~element()
{
    destroy();
}

Thank you.

PS:
Here is a sample main:

int main()
{
   element* el_1 = new element();
   element* el_2 = new element();
   element* el_3 = new element();

   el_1.add_element(el_2);
   el_2.add_element(el_3);

   return 0;
}

Also, what if I do this (aka don’t use new):

int main()
{
   element el_1;
   element el_2;
   element el_3;

   el_1.add_element(&el_2);
   el_2.add_element(&el_3);

   return 0;
}
  • 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-28T00:58:59+00:00Added an answer on May 28, 2026 at 12:58 am
    element::~element()
    {
        typedef std::vector<element*>::const_iterator iterator;
        for (iterator it(_elements.begin()); it != _elements.end(); ++it)
            delete *it;
    }
    

    delete this in a destructor is always wrong: this is already being destroyed!

    In addition, you would need to declare a copy constructor and copy assignment operator (either leaving them undefined, making your class noncopyable, or providing a suitable definition that copies the tree).

    Alternatively (and preferably), you should use a container of smart pointers for _elements. E.g.,

    std::vector<std::unique_ptr<element>> _elements;
    

    When an element is destroyed, its _elements container will be automatically destroyed. When the container is destroyed, it will destroy each of its elements. A std::unique_ptr owns the object to which it points, and when the std::unique_ptr is destroyed, it will destroy the element to which it points.

    By using std::vector<std::unique_ptr<element>> here, you don’t need to provide your own destructor, because all of this built-in functionality takes care of the cleanup for you.

    If you want to be able to copy an element tree, you’ll still need to provide your own copy constructor and copy assignment operator that clone the tree. However, if you don’t need the tree to be copyable, you don’t need to declare the copy operations like you do if you manage the memory yourself: the std::unique_ptr container is itself not copyable, so its presence as a member variable will suppress the implicitly generated copy operations.

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

Sidebar

Related Questions

Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have a class like the following: public class Stage { public int
Suppose I have the following situation: 9 class Program 10 { 11 public static
Suppose I have: class Foo { ... }; class Bar : public Foo {
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have an interface: public interface FooInterface { public void someMethod(); } and
I have some code similar to the following: public interface IMyClass { MyEnum Value
Suppose I have ViewModel like public class AnotherViewModel { public string Name { get;

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.