object goes out of scope before = operation

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

 

I have thous objects:

Polygon p1, p2;

And I have an inheriting class of Polygon called Triangle, and I try to do:

p1 = Triangle(temp1, temp2, temp3); // temp 1,2,3 are lengths of sides

But for some reason the destructor for Triangle is called at the end of construction.

Rectangle::~Rectangle(void)
{
    Polygon::~Polygon();
}

Polygon::~Polygon(void)
{
    if (sides != NULL)
    {
        delete [] sides;
        sides = NULL;
    }
}

Then it runs the destructor for Polygon a second time.

so after the code ends this is what the debugger says about p1n is number of sides):

p1  {n=3 sides=0x0062c070 {-17891602} } Polygon

Questions:

  • Why does it call the destructor?
  • Why both Triangle and Polygon's destructor are called?
  • How can this be fixed?

EDIT: as requested:

/*returns true if for the polygons A and B:
(a) for each side on polygon A there is an equal side on polygon B
(b) the number of sides in polygons A and B are equal
(c) sum of sides of A equals the sum of sides of B */
bool Polygon::operator==(const Polygon& p) const
{
    if (n != p.n)
        return false;
    if(circumference() != p.circumference())
        return false;
    for (int i=0; i < n; i++)
        if (!doesSideHasEqual(sides[i], p, n))
            return false;
    return true;
}

Also, thanks for the explanation why it ran ~Polygon, will take into account.

profilepic.png
manpreet 2 years ago
Polygon p1;

p1 = Triangle(temp1, temp2, temp3); // temp 1,2,3 are lengths of sides

is in fact giving you result that is out of your expectation.

What you want to do is something like

Polygon& p1;
Triangle t(temp1, temp2, temp3);

p1 = t;

or

Polygon* p1 = new Triangle(temp1, temp2, temp3);

The problem in

p1 = Triangle(temp1, temp2, temp3);

is not only the Triangle object is temporary and destroyed right after the statement, but, more seriously, the copy from the Triangle object to p1 which is the Polygon, is only copying as is a Polygon. Therefore, the "copied" p1 is not containing any com/tag/information">information of Triangle.


Another problem is the way you write the destructor. Destructor in C++ is automatically chained (I think you are coming from Java com/tag/background">background which we need to explicitly invoke super class' finalizer)

Rectangle::~Rectangle(void)
{
    Polygon::~Polygon();  // can be removed
}

Therefore you shouldn't call Polygon's destructor.

Another thing about destructor is, if your class is supposed to be inherited (your Polygon is a good example), you should declare your destructor to be virtual. For the reason behind, search for "virtual destructor".


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.