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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:14:17+00:00 2026-05-11T16:14:17+00:00

The sentence below is from, The Positive Legacy of C++ and Java by Bruce

  • 0

The sentence below is from, The Positive Legacy of C++ and Java by Bruce Eckel, about operator overloading in C++:

C++ has both stack allocation and heap
allocation and you must overload your
operators to handle all situations and
not cause memory leaks. Difficult
indeed.

I do not understand how operator overloading has anything to do with memory allocation. Can anyone please explain how they are correlated?

  • 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-11T16:14:17+00:00Added an answer on May 11, 2026 at 4:14 pm

    I can imagine a couple possible interpretations:

    First, in C++ new and delete are both actually operators; if you choose to provide custom allocation behavior for an object by overloading these operators, you must be very careful in doing so to ensure you don’t introduce leaks.

    Second, some types of objects require that you overload operator= to avoid memory management bugs. For example, if you have a reference counting smart pointer object (like the Boost shared_ptr), you must implement operator=, and you must be sure to do so correctly. Consider this broken example:

    template <class T>
    class RefCountedPtr {
    public:
        RefCountedPtr(T *data) : mData(data) { mData->incrRefCount(); }
        ~RefCountedPtr() { mData->decrRefCount(); }
        RefCountedPtr<T>& operator=(const RefCountedPtr<T>& other) {
            mData = other.mData;
            return *this;
        }
        ...
    protected:
        T *mData;
    };
    

    The operator= implementation here is broken because it doesn’t manage the reference counts on mData and other.mData: it does not decrement the reference count on mData, leading to a leak; and it does not increment the reference count on other.mData, leading to a possible memory fault down the road because the object being pointed to could be deleted before all the actual references are gone.

    Note that if you do not explicitly declare your own operator= for your classes, the compiler will provide a default implementation which has behavior identical to the implementation shown here — that is, completely broken for this particular case.

    So as the article says — in some cases you must overload operators, and you must be careful to handle all situations correctly.

    EDIT: Sorry, I didn’t realize that the reference was an online article, rather than a book. Even after reading the full article it’s not clear what was intended, but I think Eckel was probably referring to situations like the second one I described above.

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

Sidebar

Related Questions

I'm working on a school assignment about Java reflection. The details are below: Write
I'm studing C++ and I can't understand the meaning of the boldface sentence below:
How do I parse sentence case phrases from a passage. For example from this
I read below line in one book about buffered input Why have buffers? First,
Below is a script written in JavaScript, the purpose is to take a sentence
How do you calculate number of letters in a sentence like below using PHP?
I have the XML given below. From this XML, in an ASPX page with
On this blog post , there is a sentence as below: This hash is
In this sentence: myCommand.ExecuteReader(CommandBehavior.CloseConnection) does it close connection in case of exception?
The last sentence in the article caught my eye [F]or C/C++ developers and students

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.