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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:27:28+00:00 2026-05-30T23:27:28+00:00

I’m training my C++ and I’m trying to write a library that’ll be able

  • 0

I’m training my C++ and I’m trying to write a library that’ll be able to represent the following number using linked lists:

999999999 * ( [i=0]Σ[999999999] 1000000000 ^ i )

For example if my number was 711381450277869054011, it’ll be represented like this:

711 * 1000000000^2 + 381450277 * 1000000000^1 + 869054011 * 1000000000^0

So, here is the struct of my LL and it’s functions:

typedef struct node* ll;
struct node
{
    unsigned int data;
    ll next;
};

bool insert(ll&, unsigned int);
// ...
void copy(ll&, ll);
void destroy(ll&);

And here is my class of unsigned very long integers:

class uli
{
public:
    uli();
    ~uli();

    // <<, >>, =, operators...

    uli& operator +=(const uli&);
    uli& operator +=(char*);
    const uli operator +(const uli&);

private:
    ll head;
};

uli::uli()
{
    head = NULL;

    insert(head, 0);
}

uli::~uli()
{
    destroy(head);
}

+= operator works fine and I’m using it to overload the + operator.

The problem is that I can’t make the method operator+() return a const uli without it being destroyed by the deconstructor before I get to use it.

const uli uli::operator +(const uli& rarg) // rarg: right argument
{
    uli larg; // left argument

    larg = *this;

    larg += rarg;

    return larg;
}
// returns an LL to somewhere ???? larg was destroyed.

Now I can’t do Z = X + Y;

  • 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-30T23:27:29+00:00Added an answer on May 30, 2026 at 11:27 pm

    You need to follow the Rule of Three,

    If your class needs either

    • a copy constructor,
    • an assignment operator,
    • or a destructor,

    then it is likely to need all three of them.

    Root Cause of your problems:
    In your case When you return a class uli instance by copy from overloaded operator + the compiler generated implicit copy constructor is used and it makes a shallow copy of your pointers, thus multiple pointers point to the same memory address and when one/some of them(temporary objects) gets destroyed the pointed to memory is deallocated through the destructor and that leaves others pointers (dangling)pointing to memory/content that does not exist.

    Solution:
    Follow the Rule of Three and implement your own copy constructor to perform Deep Copying of the involved pointers.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.