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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:40:55+00:00 2026-05-28T16:40:55+00:00

This is something I can’t understand. Let’s say that I overload operator & to

  • 0

This is something I can’t understand.
Let’s say that I overload operator & to make and and between two objects, and operator = to make assignments (making a copy of the object).
Let’s suppose that I have a class named A, and this code:

A a1,a2,a3;
// initialize a1,a2,a3
a1=a2&a3;

If a1 was already allocated, when I assign a1 to a2&a3, there is a memory leak?
is the new object created without deleting the old one ?

PS: Operators overloading using two arguments of type A (I haven’t defined a class A, this is an example), returning an arguments of type A, defined as friend of the class.

The code:

It’s a very long code, so I used pastebin:
http://pastebin.com/42TnThfC
But since it’s long, I also post the most significative parts:

template<class T>
List<T>& List<T>::operator= (List<T>& l)
{
    List<T>* ptr=l.next;
    if(this!=&l)
    {
        resize(0);
        while(!ptr->end)
        {
            push(ptr->info);
            ptr=ptr->next;
        }
    }
    return *this;
}

template <class T>
List<T>& operator& (List<T>& l1, List<T>& l2) throw()
{
    List<T>* temp,*ptr=l1.next;
    temp=new List<T>();
    try
    {
        if( (l1.end)^(l2.end) )
            throw excp1;
    }
    catch (char *s)
    {
        cout << "Exception: "<<s<<endl;
    }
    if(l1.end)
    {
        while(!ptr->end)
        {
            if(l2.in(ptr->info))
                temp->push(ptr->info);
            ptr=ptr->next;
        }
    }
    else
    {
        if(l1.info==l2.info)
            temp->push(l1.info);
    }
    return *temp;
}

It’s a list, push pushes an item, an example of main:

int main(int arcg, char **argv)
{
    List<T>l1,l2,l3;
    for(int i=0;i<10;i++)
    {
        l1.push(i);
        l2.push(i);
        l3.push(i);
    }
    l3=l1&l2;
}

This case l3 is already l1&l2, but will this cause a memory leak?

  • 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-28T16:40:57+00:00Added an answer on May 28, 2026 at 4:40 pm

    No, it should be fine. The original a1 will be destroyed and replaced with a new one (which is the result of a2 & a3).

    However you should ensure that, if A contains pointers, the copy constructor, assignment operator and destructor are properly defined for that type, otherwise “the original a1 will be destroyed” may not be enough!

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

Sidebar

Related Questions

I can do something like this: validator.showErrors({ nameOfField : ErrorMessage }); And that works
I want to find a sql command or something that can do this where
Is this something that can be used for highly secure information or should it
Let's say that I created a video with something like Camtasia and saved it
Can someone explain why everything in WPF is blurry? Is this something that can
Is this something that can be done more efficiently than with a vertex array?
This is something I know can be done somehow , because I've done it
How I can do something like this in C++: void my_print(format_string) { vector<string> data;
I know you can do something like this: readlines(FileName) -> {ok, Device} = file:open(FileName,
This is something I've always wondered, and I can't find any mention of it

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.