class A: public B
{
C* p1;
C* p2;
}

What would be a correct assignment operator for class A? We have to ensure that we do not leave any object in an inconsistent state due to any exceptions or create any dangling references or memory leaks.

You did not post anything that needed an assignment operator =. Assignment operators are not used in class declarations. Maybe you mean the overloaded = operator???

Your class doesn't have any public members, so none of its objects (p1 and p2) can be accessed outside the class.

I want to define an assignment operator for my the class A. something like

A& operator=(const A& right)
{
//not too sure on how to copy the pointers correctly here.
 return *this;
}

I hope this would have made my problem clear.

You could just say p1 = right.p1; but that could be dangerous if right ever goes out of scope or is destroyed. A safer approach is to allocate new memory for p1 and copy the contents of right.p1 into it.

A& operator=(const A& right)
{
    A a;
    a.p1 = new C;
    if( right.p1 != NULL)
       *a.p1 = *right.p1;
    return a;
}

So what happens when this function returns?

//suppose i have 2 objects of A somewhere above c,d,
//i assign d to c;
c = d;

'this' (c) gets overwritten by this new object 'a' that we are returning? If that's true then if c.p1 was already pointing to a valid object wouldn't that get leaked? Also when i return 'a' instead of 'this' would it need a copy ctor to copy it into 'c' ?

Dave: yes i have seen those links. Unfortunately they handle the normal cases which I already know. This question was given to me an interview an i gave this ans

A& operator=(const& A right)
{
         if(this != &right)
         {
              p1 = new C(right.p1);
              p2 = new C(right.p2);
          }
         
          return *this;
}

the interviewer didn't tell me if it was right or wrong :( .. came home, tried to work through it and realised that it could have a lot of potential errors like exceptions in assigning memory, memory leaks etc and hence this post. Just want to understand it better.

Dave: yes i have seen those links. Unfortunately they handle the normal cases which I already know.

I'm not sure what you're saying.

In the C++ FAQ example, Fred has one pointer instead of your two. Other than that, what do you find so different about the problem? What is "normal" about this and "special" about yours?

So what happens when this function returns?

//suppose i have 2 objects of A somewhere above c,d,
//i assign d to c;
c = d;
A& operator=(const& A right)
{
         if(this != &right)
         {
              p1 = new C(right.p1);
              p2 = new C(right.p2);
          }
         
          return *this;
}

'this' (c) gets overwritten by this new object 'a' that we are returning? If that's true then if c.p1 was already pointing to a valid object wouldn't that get leaked? Also when i return 'a' instead of 'this' would it need a copy ctor to copy it into 'c' ?

The example given is handling self-assignment (it even goes into details about special-casing it with the if). It is handling the memory cleanup.

Fred& Fred::operator= (const Fred& f)
 {
   // This code gracefully (albeit implicitly) handles self assignment
   Wilma* tmp = new Wilma(*f.p_);   // It would be OK if an exception got thrown here
   delete p_;
   p_ = tmp;
   return *this;
 }

Part of you question seems to be asking about the copy constructor of your C or this example's Wilma. Yes, the copy constructor is involved. Again I must be confused about what you are asking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.