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

  • Home
  • SEARCH
  • 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 997441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:02:06+00:00 2026-05-16T07:02:06+00:00

Take a simple class with the big 3 (constructor, copy constructor, destructor): #include <vector>

  • 0

Take a simple class with the “big 3” (constructor, copy constructor, destructor):

#include <vector>
using namespace std; //actually goes in the C file that links to this header file
...
class planets(){ //stores mass and radii data for planets in a solar system.
   public:
      vector <double> mass;
      vector <double> radius;

   //constructor
   planets( int numObj ){
     for(int i=0; i<numObj; i++){
         mass.push_back(8.0); //some default values.
         radius.push_back(2.0);
     }
   }
   //copy constructor
   planets(const planets &p){
      vector <double> mass(p.mass); //copy vectors into new class.
      vector <double> radius(p.radius);
   }
  //destructor
  ~planets(){
     delete mass; //ERROR: (...) argument given to ‘delete’, expected pointer
     ~radius(); //also causes error: no match for call to(...) 
   }
}

I plan on making a vector of planets, thus the need for the “big 3”:

vector <planets> stars;
stars.push_back(planets(5)); //5 hypothetical planets of alpha centauri
stars.push_back(planets(8)); //our solar system. Used to be nine.
///etc.

How do I delete the mass and radius vectors properly, to avoid memory leaks (do I even have to)?

  • 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-16T07:02:07+00:00Added an answer on May 16, 2026 at 7:02 am

    No, you don’t need to do anything because you aren’t managing any resources. You only write the Big Three when you’re managing a resource, but vector is doing that. It’s the one with the Big Three properly written, you just use it.

    This is why the single responsibility principle is key in resource management: once you have some class that properly manages a resource, you can simply use it without ever worrying about that resource again. Always split resource management from resource use.

    The reason you need the Big Three written in a managing class is because the default special members typically do the wrong thing (they copy, assign, destruct values instead of what the values manage/point at.) But once you’re resource is wrapped up (like in a std::vector), everything is just fine. The defaults will copy vector, but that copying is correctly written.

    By the way, the Big Three is in the context of managing resources (copying and destroying resources), not created them. So it would be copy-constructor, copy-assignment, and destructor, not default constructor.


    For your information, here’s how you would do it:

    class planets
    {
    public:
        // ...
    
        //copy constructor
        planets(const planets &p) : // use an initialization list to initialize
        mass(p.mass), // copy-construct mass with p.mass
        radius(p.radius) // copy-construct radius with p.radius
        {
            // what you had before just made a local variable, copy-constructed
            // it with p.xxx, then got released (nothing happened to your members)
        }
    
        //destructor
        ~planets()
        {
            // nothing to do, really, since vector destructs everything 
            // right for you, but you yes, you would delete any resources
            // you managed here
        }
    };
    

    But don’t forget the copy-assignment operator. I recommend the copy-and-swap idiom, and leave that as an exercise for you.

    (Remember you don’t actually need these, though.)

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

Sidebar

Related Questions

take this simple code: class A{ public: virtual void foo() = 0; void x(){
Take a simple XML file formatted like this: <Lists> <List> <Note/> ... <Note/> </List>
I'm developping a simple widget which update by downloading a big image file. To
I'm developing something like a simple and purpose-specific cms. I'm using a db class
take this simple code: <div id=container> <div class=box>abc</div> <div class=box id=secondbox>abc</div> <div>generic</div> <div>generic</div> </div>
Take this simple example: class Program { static void Main(string[] args) { var windsorContainer
Please take a look at the following simple code: class Foo { public: Foo(){}
take a look at following simple code : class A; class B { A
Take the following simple example: interface IVehicle { } class Car : IVehicle {
Hopefully a simple question. Take for instance a Circularly-linked list: class ListContainer { private

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.