Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Actually, I tried to implement the assignment operator but it fails. Can you help me?

C++
class A
{
   public:
      int *a1;

      A()   { a1 = new int; cout<<__LINE__<<endl;   }

      A(const A *a)
      {
         if(a1)
            delete a1;
         a1 = new int;
         *a1 = *(a->a1);
         cout<<__LINE__<<endl;
      };

      A& operator = (const A &a)
      {
         if(a1)
            delete a1;
         a1 = new int;
         //a1 = *(a.a1);
         cout<<__LINE__<<endl;
         return *this;
      }
};

int main()
{
   A *a1 = new A();
   *(a1->a1) = 99;
   A *b1 = new A(a1);
   *(b1->a1) = 77;
   b1 = a1;
   cout<<*(a1->a1)<<","<<*(b1->a1)<<endl;
   cout<<a1<<","<<b1<<endl;
}


What I have tried:

Gets succeed in compile time = operator.
Posted
Updated 6-Nov-23 4:13am
v2
Comments
OriginalGriff 25-Oct-23 1:01am    
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.
jeron1 25-Oct-23 10:11am    
What's wrong with the answers you received the first time you asked this?
https://www.codeproject.com/Questions/5369930/How-to-implement-the-deep-copy-constructor-using-p

Clean up your code with better naming so the usage of "A" for a class name is very bad style. Use some word. And the same for the member "a". You also dont need to use a pointer for an int. That make the code much easier to understand.

I miss the assigment of your a1 member in the
C++
A& operator = (const A &a)
after the creation.

But write better code. It belongs to every code. Nobody wants to fizzle in that mess.
 
Share this answer
 
This is from a solution I posted previously for almost exactly the same question. You have fewer members this time. My answer is essentially the same this time, too. The main difference between yours and mine is I route most of it through a Set method so that I avoid duplicating code. That makes debugging much easier.

Here is the revised answer :
C++
class A
{
public:
    int * a1 { nullptr };

    A() {}                  // default constructor

    A( int x )              // assignment constructor
    {
        Set( x );
    }

    A( const A & other )    // copy constructor
    {
        Set( * other.a1 );
    }

    ~A()                        // destructor
    {
        delete a1;
        a1 = nullptr;
    }

    // copy operator

    A & operator = ( const A & other )
    {
        // prevent self-assignment

        if( this != & other )
        {
            // do copy operation

            Set( * other.a1 );
        }
        return *this;
    }

    void Set( int x )   // assignment method
    {
        if( a1 )
            delete a1;
        a1 = new int( x );
    }
};
I think it is pointless (no pun intended) to pass a pointer to an integer as an argument if you are only going access its value so I have it passed by value, not address.
 
Share this answer
 
v2
Comments
CPallini 25-Oct-23 1:59am    
My 5.
Maxim Kartavenkov 25-Oct-23 3:42am    
Add Set(0) in default constructor -to allocate a1 pointer and set 0 value as in example he creates default object and assign value to a1 after.
Also add destructor to show how to free memory.
Rick York 25-Oct-23 10:42am    
No and STOP GIVING ME ORDERS!!! The default value is set for the member variable and that is adequate. A destructor would be useful though. Try being less rude in the future.
Maxim Kartavenkov 25-Oct-23 11:53am    
You didn't look at the code in the question:
 A *a1 = new A();
*(a1->a1) = 99;

Your code will crash on assignment.
Just make you note for improving your solution to become perfect instead of posting my own which is not needed here.
Rick York 25-Oct-23 13:03pm    
That is NOT my code. What I posted does not have a main function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900