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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:38:51+00:00 2026-06-05T07:38:51+00:00

Imagine the simple class line segment which it’s constructor is are couple of objects

  • 0

Imagine the simple class line segment which it’s constructor is are couple of objects from the class point:

class Point ;

class LineSegment {
    public:

      LineSegment(Point* P1, Point* P2);
      ~LineSegment();
} ;

The question is, in the destructor, should I delete the points first or it will be done automatically? On the other words, in the cpp file, which of the following should be written:

LineSegment::~LineSegment()

or

LineSegment::~LineSegment(){
     delete P1;
     delete P2;
}
  • 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-06-05T07:38:53+00:00Added an answer on June 5, 2026 at 7:38 am

    First, note that

    LineSegment::~LineSegment(){
         ~P1();
         ~P2();
    }
    

    is invalid. To delete, you need delete P1 and delete P2.

    Also, figure out who owns P1 and P2. If they are owned by the class, delete them in the destructor, but note that you won’t be able to access them outside after the object is destroyed.

    So, for example:

    Point* p1 = new Point;
    Point* p2 = new Point;
    
    {
       LineSegment l(p1,p2);
    }
    
    delete p1;
    delete p2;
    

    Would be illegal if you delete the pointers in the destructor.

    Either version you choose, be sure to document your decision – i.e. make it clear whether the class assumes ownership of the pointers or not.

    You wouldn’t have this problem if you’d use smart pointers.

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

Sidebar

Related Questions

Imagine the following two entities. Element is simple class containing some data: @Entity public
Imagine this simple piece of code: class Constructor() { var string: String = _
My question is so simple. Imagine we have a Foo class. and has a
excuse me for my ugly english) ! Imagine these very simple models : class
Imagine a simple case like this: class Book has_many :chapters end Let's say in
Imagine I have a class that represents a simple washing machine. It can perform
Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
I will give very simple example. class Implementation: IMyInterface { string mArg; public Implementation(string
I have a simple container class with a copy constructor. Do you recommend using
Imagine this simple form <form action=<?php echo $_SERVER['REQUEST_URI']; ?> method=post> <fieldset> <legend>Contact Me</legend> <label

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.