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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:02:59+00:00 2026-06-03T05:02:59+00:00

Suppose one has this code: class MyClass { public: MyClass(int foo) : foo(foo){} private:

  • 0

Suppose one has this code:

class MyClass {
public:
    MyClass(int foo) : foo(foo){}

private:
    int foo;

//overloaded operator +
public:
    MyClass operator+(MyClass &mc){
        MyClass c(mc.foo + foo);
        return c;
    }
};

int main(int argc, char* argv[]){
    MyClass c1(10);
    MyClass c2(12);

    c2 = c1 + c2;

    return EXIT_SUCCESS;
}

Where the operator + is overload such that it does not modify the object but create a new object and return it.

When c2 = c1 + c2 is called, c2 is bound with the new object, but the previous object bound with c2 is not (or at least it seems to me) freed. I’m i right?

Since C++ has no a Garbage Collector, is this a problem?

Am I missing something?

  • 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-03T05:03:01+00:00Added an answer on June 3, 2026 at 5:03 am

    When c2 = c1 + c2 is called, c2 is bound with the new object, but the previous object bound with c2 is not (or at least it seems to me) freed.

    No, that’s not what happens.

    There is no “previous object bound with c2“; c2 is not a reference to an object, it is the object. c2‘s lifetime begins when it is declared and ends when it goes out of scope.

    The assignment operator doesn’t rebind c2 (which would be meaningless — c2 isn’t a java-style reference!), it assigns to it. Specifically, it invokes c2.operator=(MyClass) or c2.operator=(const MyClass&). c2 existed before the assignment, and the same object continues to exist after the assignment.

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

Sidebar

Related Questions

Suppose I have this code: class A { }; class B: virtual public A
Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void
Suppose one wants to build a novel generic class, Novel[A] . This class will
Suppose I have a class with a private property and associated public getter and
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah
Suppose one has about 50,000 different .cpp files. Each .cpp file contains just one
Suppose I have two tables that are linked (one has a foreign key to
I tried function: strstr, but it has one problem. Suppose, the URL looks like:
Suppose I have 2 models. The 2nd model has a one-to-one relationship with the

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.