very confused about what happens when we reinitialize the object in c++

这一生的挚爱 提交于 2020-01-17 15:12:10

问题


#include<iostream>
#include<string.h>
using namespace std;
struct integer
{
int a;
integer *next;
integer *prev;
};
integer *temp,*temp1,*temp2;
class Myinteger
{
private:


public:
  integer *head;
 int length;
integer *tail;
Myinteger()
{
    cout<<"constructed started"<<"\n";
    length=1;
    temp = new integer;
    head=temp;
    tail=temp;
    temp->prev=NULL;
    temp->next=NULL;
    temp->a=0;
    cout<<"constructed ended"<<"\n";
}
Myinteger (string s)
{
    cout<<"constructor started string "<<"\n";
    int i=0,flag=0;
    temp=new integer;
    head=temp;
    temp->prev=NULL;
    if(s[0]=='-')
    {
        temp->a=-(s[1]-48);
        i=i+2;
        while(i<s.length()-1)
    {
        temp1=new integer;
        temp->next=temp1;
        temp1->prev=temp;
        temp1->a=s[i]-48;
        temp=temp1;
        i++;
    }
    if(s.length()>2)
    {
        temp1=new integer;
        temp->next=temp1;
        temp1->prev=temp;
        temp1->next=NULL;
        temp1->a=s[i]-48;
        tail=temp1;
        if(s.length()==3)
            length=i;
        else
            length=i;
    }
     else
     {
         temp->next=NULL;
         tail=temp;
         length=i-1;
     }
    }
    else
    {
         temp->a=s[0]-48;
         i++;
         while(i<s.length()-1)
    {
        temp1=new integer;
        temp->next=temp1;
        temp1->prev=temp;
        temp1->a=s[i]-48;
        temp=temp1;
        i++;
    }
    if(s.length()>1)
    {
        temp1=new integer;
        temp->next=temp1;
        temp1->prev=temp;
        temp1->a=s[i]-48;
        temp1->next=NULL;
        tail=temp1;
        if(flag==0)
        length=i+1;
    }
    else
    {
        tail=temp;
        temp->next=NULL;
        if(flag==0)
        length=i;
    }
    }
    cout<<"constructed ended string"<<"\n";
}
Myinteger(const Myinteger& obj)
{
    cout<<"copy constructor called"<<"\n";
    int i;
    length=obj.length;
    temp1=obj.head;
    temp=new integer;
    head=temp;
    temp->prev=NULL;
    temp->a=temp1->a;
    temp1=temp1->next;
    for(i=0;i<obj.length-2;i++)
    {
        temp2=new integer;
        temp->next=temp2;
        temp2->prev=temp;
        temp2->a=temp1->a;
        temp1=temp1->next;
        temp=temp2;
    }
    if(obj.length>1)
    {
        temp2=new integer;
        temp->next=temp2;
        temp2->prev=temp;
        temp2->a=temp1->a;
        temp2->next=NULL;
        tail=temp2;
    }
    else
    {
        tail=temp;
        temp->next=NULL;
    }
    cout<<"copy constructed ended"<<"\n";
}
~Myinteger()
{
    cout<<"destructor started"<<"\n";
    integer *del=head;
    cout<<"hiii";
    for(int i=0;i<length;i++)
    {
        temp1=del->next;
        cout<<del<<" "<<del->a<<"\n";
        //delete del;
        //del=NULL;
        del=temp1;
    }
    //head=tail=NULL;
    cout<<"destructor ended"<<"\n";

}
static Myinteger parse(string s)
{
     Myinteger a(s);
     return a;
}
void printint()
{
     temp=head;
     while(temp!=NULL)
     {
         cout<<temp->a;
         temp=temp->next;
     }
}

Myinteger operator-( const Myinteger &obj)
{

  int borrow=0,flag,length,flag1;
  int a=this->length;
  int b=obj.length;
  integer *tem1;
  integer *tem2;
  Myinteger temp=obj;
  if(this->head->a<0&&temp.head->a>0)
  {
      this->abs();
      Myinteger ret = *this+temp;
      ret.head->a=-(ret.head->a);
      return ret;
  }
  else if(this->head->a>0&&temp.head->a<0)
  {
      temp.abs();
      return *this+temp;
  }
  else if(this->head->a<0&&temp.head->a<0)
  {
      this->abs();
      temp.abs();
      flag1=-1;
  }
  else
  {
      flag=0;
  }
  if(a>=b)
  {
       length=this->length;
       flag=0;
       tem1=this->tail;
       tem2=temp.tail;
       integer *tem3=this->head;
       integer *tem4=temp.head;
       if(a==b)
       for(int i=a;i>0;i--)
       {
           if(tem3->a>tem4->a)
           {
               tem1=this->tail;
               tem2=temp.tail;
               break;
           }
           else if(tem3->a<tem4->a)
           {
               tem1=temp.tail;
               tem2=this->tail;
               flag=-1;
               break;
           }
           else
           {
               tem3=tem3->next;
               tem4=tem4->next;
           }
       }
  }
  else
  {
       tem2=this->tail;
       tem1=temp.tail;
       length=temp.length;
       flag=-1;
  }
  char s[max(a,b)+2];
  int m=max(a,b)+1;
  s[m-1]='\0';
  m=m-2;
  while(tem1!=NULL&&tem2!=NULL)
  {
      if((tem1->a-tem2->a+borrow)<0)
      {
          s[m]=((tem1->a-tem2->a)+10+borrow)+48;
          tem1=tem1->prev;
          tem2=tem2->prev;
          m--;
          borrow=-1;
      }
      else
     {
        s[m]=(tem1->a-tem2->a+borrow)+48;
        tem1=tem1->prev;
        tem2=tem2->prev;
        m--;
        borrow=0;
     }
  }
      while(tem1!=NULL)
      {
          if(tem1->a+borrow<0)
          {
              s[m]=(tem1->a+borrow+10)+48;
              tem1=tem1->prev;
              m--;
              borrow=-1;
          }
          else
          {
              s[m]=(tem1->a+borrow)+48;
              tem1=tem1->prev;
              m--;
              borrow=0;
          }
      }
      while(s[0]==48)
      {
          int i=0;
          if(length==1)
            break;
          while(i<=length)
          {
              s[i]=s[i+1];
              i++;
          }
          length--;
      }
      if(flag==-1)
      {
          int i=length;
          while(i>=0)
          {
              s[i+1]=s[i];
              i--;
          }
          s[0]='-';
      }
  string as(s);
  Myinteger ret(as);
  if(flag==-1&&flag==0)
    ret.head->a=-(ret.head->a);
  return ret;
 }
 Myinteger operator+( const Myinteger &obj)
 {
  int flag=0;
  int carry=0;
  int length;
  int a=this->length;
  int b=obj.length;
  integer *tem1;
  integer *tem2;
   Myinteger temp=obj;
  if(this->head->a<0&&temp.head->a>0)
  {
    this->abs();
    Myinteger ret =*this-temp;
    ret.head->a=-(ret.head->a);
    return ret;
  }
  else if(this->head->a>0&&temp.head->a<0)
  {
      temp.abs();
      return *this-temp;
  }
  else if(this->head->a<0&&temp.head->a<0)
  {
      this->abs();
      temp.abs();
      flag=-1;
  }
  else
  {
      flag=0;
  }
  if(a>=b)
  {
       tem1=this->tail;
       tem2=temp.tail;
       length=this->length;
  }
  else
  {
       tem2=this->tail;
       tem1=temp.tail;
       length=temp.length;
  }
  char s[max(a,b)+2];
  int m=max(a,b)+1;
  s[m-1]='\0';
  m=m-2;
  while(tem1!=NULL&&tem2!=NULL)
  {
      if((tem1->a+tem2->a+carry)>9)
      {
          s[m]=((tem1->a+tem2->a)-10+carry)+48;
          tem1=tem1->prev;
          tem2=tem2->prev;
          m--;
          carry=1;
      }
      else
     {
        s[m]=(tem1->a+tem2->a+carry)+48;
        tem1=tem1->prev;
        tem2=tem2->prev;
        m--;
        carry=0;
     }
  }
      while(tem1!=NULL)
      {
          if(tem1->a+carry>9)
          {
              s[m]=(tem1->a+carry-10)+48;
              tem1=tem1->prev;
              m--;
              carry=1;
          }
          else
          {
              s[m]=(tem1->a+carry)+48;
              tem1=tem1->prev;
              m--;
              carry=0;
          }
      }
      if(carry==1)
      {
         int i=length;
          while(i>=0)
          {
             s[i+1]=s[i];
             i--;
          }
          s[0]=49;
      }
  string as(s);
  Myinteger ret(as);
  if(flag==-1)
  {
      ret.head->a=-(ret.head->a);
  }
  return ret;
  }
   void abs()
   {
 if(head->a<0)
 {
     head->a=-(head->a);
 }
   }
   Myinteger operator *(const Myinteger &obj)
  {
  Myinteger temp1=obj;
  Myinteger temp2("0");
  temp2.printint();
  Myinteger temp3;
  //integer *tem1=tail;
  integer *tem2=temp1.tail;
  while(tem2!=NULL)
  {
      temp2.printint();
      int multi=tem2->a;
      //cout<<multi;
      //cout<<temp2.tail->next;
      for(int i=0;i<1;i++)
      {
         // cout<<temp2.tail->next;
          temp3=*this+temp2;
          temp3.printint();
          //cout<<temp2.head->a;
          temp2=temp3;
      }
      tem2=tem2->prev;
  }
  //temp2.printint();
  return temp2;
  }
  };
  int main()
 {
Myinteger obj2;
obj2=Myinteger("10");
cout<<"hiii";
obj2=Myinteger("11");
return 0;
}

` a destructor is being called immediately after reinitializing the object obj2 with "10",ie before printing 10,also obj2 destructor is called three times int the program which should lead to error if my object has pointers . please ,help me to solve this issue


回答1:


What you are doing here is called assignment. You create a new Myinteger object with parameter "10" and assign it to an existing Myinteger object, obj2.

What's called here is the assignment operator of obj2, which if you didn't define yourself, is automatically defined for you by assigning each of the internal fields.

So obj2 = Myinteger("10"); creates a Myinteger and assigns it to obj2, and after that it destructs the temporary object in the process. So, the temporary result of Myinteger("10") is destructed after it has been assigned to obj2.

The same goes for obj2 = Myinteger("11");. Finally, obj2 gets destructed when it goes out of scope, at the end of main.

EDIT: I realized I didn't address what might be the better way to deal with it. Not sure about your specific case, but I'll elaborate on one approach. If you use C++11, you can implement the move constructor and move assignment operator. If you implement those correctly, temporary objects will not assigned using the assignment operator, but by the move assignment operator. In that operator, you can implement it efficiently. There are rules when the move constructor and assignment operator are called implicitly, but it's also possible to use std::move to force it.

For example, a move assignment of a vector can just transfer the pointer to the underlying array instead of copying the entire array.

Another example is if you have a network connection for each object. You can decide that in the target object, you use the original connection and close the one on the temporary object. If you have an connect method for the connection and it was not called - then you even haven't wasted any resources yet.

Note that you'll still get 3 constructor and destructor calls, but at least you'll be more efficient dealing with the underlying fields.



来源:https://stackoverflow.com/questions/24593021/very-confused-about-what-happens-when-we-reinitialize-the-object-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!