1

I have the below code for class A

class A
{

int *ptr;

    public:
       A();
       A(const A &);
       ~A();
       A&  operator = (const A &);
       void display();
};

void A::display()
{

cout<<" ptr ="<<*ptr<<endl;
}

A::A()
{
    cout<<"A's constructor called"<<endl;
    ptr = new int;
    *ptr = 100;
}

A::A(const A &src)
{
    cout<<"copy constructor called"<<endl;
    ptr = new int;
    *ptr = *src.ptr;
}

A::~A()
{
    delete ptr;
    cout<<" destructor called"<<endl;
}

A&  A::operator = (const A &src)
{
    cout<<"A::assignmnet operator called"<<endl;
  if(&src != this)
  {

   delete ptr ;
   ptr = new int;
   *ptr = *src.ptr;

  }

    return *this;

}  

Now there is another class B which contains a pointer to class A as a member variable

class B
{
    A *a;
    public:
       B()
       {
           cout<<" B's constructor called"<<endl;
           a = new A();
       }

    B& operator = (const B &);
    ~B()
    {
        cout<<"B:destructor called"<<endl;
        delete a;
    }

    void display()
    {
     cout<<"inside B's display"<<endl;
     a->display();   
    }

};

Now the assignment operator for class B can be written as 1.

B& B::operator=( const B & src)
{

 cout<<"B's assignment operator called"<<endl;
    if(this != &src)
    {
         *a = *src.a;
    }

    return *this;

 }

Or as 2.

 B& B::operator=( const B & src)
{

 cout<<"B's assignment operator called"<<endl;
    if(this != &src)
    {

        delete a;
        a = new A();
        *a = *src.a;
    }

    return *this;

 } 

Are these two scenario correct.

2
  • 1
    Where is the difference between the scenarios? Commented Nov 11, 2016 at 10:04
  • And why is a a pointer when it's owned by the class? Commented Nov 11, 2016 at 10:05

1 Answer 1

2

In the code you show, a should not be a pointer but simply declared as A a;.

From your code B has the ownership of A. The reason is twofolds:

  • you explicitly create A objects in B and not derived types of A

  • B has the ownership of a: constructors, destructor and assignment.

If you need some kind of polymorphism on A (use derived types of A), you can use smart pointers with std::unique_ptr.

Your first scenario reasons as if the class B contains A a. Your second scenario reasons as if the class B contains std::unique_ptr<A> a. Both scenarios are correct. But I think the first one is more consistent (and more efficient) w.r.t. the current whole code.

If you need polymorphism, you should use the second with std::unique_ptr. Note that in this second scenario, you could replace

a = new A();
*a = *src.a;

by

a = new A(*src.a);

if the copy constructor is consistent w.r.t. the assignment operator.

With A a the code would be

class B
{
    A a;
    public:
       B() {} // calls A()
       B& operator = (const B & src) // or = default;
         { a = src.a; // this != &src is managed inside a
           return *this;
         }
      ~B() {} // no more usefull
      void display()
      {
        cout<<"inside B's display"<<endl;
        a.display();   
      }
};

With unique_ptr the code would be

class B
{
    std::unique_ptr<A> a;
    public:
       B() : a(new A()) {}
       B& operator = (const B & src)
         { if (this != &src)
              a.reset(src.a.get() ? new A(*src.a) : nullptr);
           return *this;
         }
      ~B() {} // no more usefull
      void display()
      {
        cout<<"inside B's display"<<endl;
        a->display();   
      }
};
Sign up to request clarification or add additional context in comments.

1 Comment

plus 1, but please state the advantages with regard to the assignment operator (and maybe also a link or example for the assignment with a unique_ptr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.