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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:22:26+00:00 2026-06-02T20:22:26+00:00

Here is an example of a class that is made available for the +

  • 0

Here is an example of a class that is made available for the + operation.

class A
{
public:
   int *array;

   A()
   {
      array = new int[10];
   }

   ~A()
   {
      delete[] array;
   }

   A operator+ (const A &b)
   {
      A c;
      for(int i=0; i<10; i++)
         c.array[i] += array[i] + b.array[i];
      return c;
   }
};

int main()
{
   A a,b,c,d;

   /* puts some random numbers into the arrays of b,c and d */
   a = b+c+d;
}

Will a run the destructor before copying the result of b+c+d or not? If not, how do I make sure no memory is leaked?

The + operator overload is designed this way such that no operand is modified.

  • 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-02T20:22:28+00:00Added an answer on June 2, 2026 at 8:22 pm

    You need to add an equals operator to A. Also, you will likely want to create a copy constructor.

    When a becomes the return from b+c+d, the array pointer in a gets over written without delete[] ever being called on it. You need to make an operator= that deletes the array.

    An example of an operator= is below:

    A& operator=(A const& a)
    {
        if (&a != this) {
            int* tmp = this->array;
            this->array = new int[10];
            //copy a.array to this->array
            delete[] tmp;
        }
        return *this;
    }
    

    There’s a lot of subtleties in this if you’re new to operator=.

    In particular, the check whether or not a is equal to this is necessary because it’s perfectly valid to write:

    A a;
    a = a;
    

    This would cause a pointless copy, and in most cases of operator= will cause bugs.

    The other subtlety is less of a requirement than that one and more of a coding style (though a very wide spread standard). When copying something that is dynamically allocated, you always want to allocate and copy before you release. That way, if new throws an exception (or something else fails), the object is still in a stable state, though with it’s old data instead of the new expected dated.

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

Sidebar

Related Questions

Here is code example class A{ int i; public: A(int i) : i(i) {}
Here's some example code: class Obj attr :c, true def == that p '=='
Here is an example of what I would do in Visual Basic: Public Class
Here's an example of what I mean: I have an array: array('type'=>'text', 'class'=>'input', 'name'=>'username',
For example I have a file name call A.java. Here its structure: public class
I just wanted to confirm the difference here, take this as an example: class
I'm trying to comprehend ontology basics. Here's an example: car (class) 2009 VW CC
I can't get past this issue I am having. Here's a simple example: class
Here is the example with comments: class Program { // first version of structure
The fictional example here. Suppose I designed a class named IODevice. This class already

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.