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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:05:40+00:00 2026-06-08T21:05:40+00:00

When overloading the = operator, should one make the contents of one object equal

  • 0

When overloading the = operator, should one make the contents of one object equal to the contents of the other object OR do you make the pointer of the object point to the same object?

Reading back on the question it seems that the contents should be copied and not the pointers. But I just can’t figure it out, So I would be grateful if someone would explain what I should do, I know how to do both, I’m just not sure which one to choose.

class IntObject
{
private:
    int *pi_One;

public:

    IntObject(void);

    IntObject::IntObject(int const &i_one);

    ~IntObject(void);

    IntObject & operator=(const IntObject&);

};

IntObject::IntObject()
{
    pi_One = new int(0); 
}

IntObject::IntObject(int const &i_one)
{
    pi_One = new int(i_one); 
}

IntObject::~IntObject(void)
{
    delete pi_One;
}

IntObject & IntObject::operator=(const IntObject& c) {
//This copies the pointer to the ints
    this->pi_One = c.pi_One;

    return *this;  
}
  • 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-08T21:05:41+00:00Added an answer on June 8, 2026 at 9:05 pm

    It depends on what semantics you want to have in your type. If you want value semantics, then copy the contents (deep copy, as is the case in std::vector), if you want reference semantics (shallow copy, as in std::shared_ptr)

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

Sidebar

Related Questions

Could someone point out the idea of overloading operator== to perform deep object comparison
As I've understand, when overloading operator=, the return value should should be a non-const
The original question is related to overloading operator= and I like to share my
I am reading a book about C++ and more precisely about the operator overloading.
Why should would one replace the default operator new and delete with a custom
Im currently anwsering exercise questions concerning operator overloading in C++. I have a question:
I am overloading operator << to implement a stream like interface for a class:
is it possible to construct variadic arguments for function by overloading operator comma of
Do polymorphism and operator overloading mix together? You can't do polymorphism without pointers, as
My first time overloading the [] operator for something practical and I ran into

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.