Assignment operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arashmath
    New Member
    • May 2008
    • 6

    Assignment operator

    Hi,
    How can i use default assignment operator (member-by-member assignment) after overloading this operator?
    I want to access orginal version of assigment operator (=),
    but i don't know the syntax that imply compiler to use default version and not to use overload version.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    I'm not sure what you want to do with operator=. It's not usually overwritten and picking specific operator='s sounds like trouble.

    Can you give an example of what you're trying to do?

    Comment

    • arashmath
      New Member
      • May 2008
      • 6

      #3
      I have code like this:

      Code:
      #include <stdio.h>
      #include <conio.h>
      #include <mem.h>
      
      void swap_int( int& int1 , int& int2 ) // A funtion that swap tow integers.
      {
         int temp = int1;
         int1 = int2;
         int2 = temp;
      }
      
      class MyClass
      {
        private:
            double *Val_; // Value data pointer
            int ValLength_; // Lenght of value data
            int Var1_;
            int Var2_;
            int Var3_; // and may be more declaration
        public:
            MyClass() { Val_ = NULL; ValLength_ = 0; }  // Default constructor
            MyClass( int Len ) { Val_ = new double[Len]; ValLength_ = Len; }
            ~MyClass() { if(Val_!=NULL) delete[] Val_; } // Free memory
      
            MyClass& operator=(const MyClass& MC) // Here i overwrite operator =
            {
               if(Val_!=NULL) delete[] Val_;
               Val_ = new double[MC.ValLength_];
               memcpy( Val_ , MC.Val_ , sizeof(double)*MC.ValLength_ );
               ValLength_ = MC.ValLength_;
               Var1_ = MC.Var1_;
               Var2_ = MC.Var2_;
               Var3_ = MC.Var3_;
               return *this;
            }
            // Now three methods for exchange MC1 , MC2
            friend void swap1( MyClass& MC1 , MyClass& MC2 );
            friend void swap2( MyClass& MC1 , MyClass& MC2 );
            friend void swap3( MyClass& MC1 , MyClass& MC2 );
      };
      
      void swap1( MyClass& MC1 , MyClass& MC2 )
      {  /* This method performance is slow because it use overwritten version
             for assignment operator */
        MyClass temp = MC1;
        MC1 = MC2;  
        MC2 = temp;
      }
      
      void swap2( MyClass& MC1 , MyClass& MC2 )
      {  // This one is much faster than swap1 and its result is same as swap1.
        double *temp = MC1.Val_;
        MC1.Val_ = MC2.Val_;
        MC2.Val_ = temp;
        swap_int( MC1.ValLength_ , MC2.ValLength_ );
        swap_int( MC1.Var1_ , MC2.Var1_ );
        swap_int( MC1.Var2_ , MC2.Var2_ );
        swap_int( MC1.Var3_ , MC2.Var3_ );
      }
      void swap3( MyClass& MC1 , MyClass& MC2 )
      {  /* Here i try improve swap2.
            if there was no user-defined operator =, the operator = would define by
            default as a member-by-member assignment of the members of class.
            But in MyClass operator = is overwritten.
            I want compliler use default version assignment in these codes:
      
      	MyClass temp = MC1;
      	MC1 = MC2;
      	MC2 = temp;
      
            How can i do this? */
      }
      
      int main()
      {
      	MyClass MC1(10000000) , MC2(5000000);
      	printf("swap1 Starts...\n");
      	swap1( MC1 , MC2 );
      	printf("swap2 Starts...\n");
      	swap2( MC1 , MC2 );
      	printf("swap3 Starts...\n");
      	swap3( MC1 , MC2 );
       	printf("Done!\n");
                      getch();
      	return 0;
      }
      I explain the problem in line 61.

      Thanks. :)

      Comment

      • mickey0
        New Member
        • Jan 2008
        • 142

        #4
        This maybe will help you; But there were some errors:
        Code:
        #ifndef MY_CLASS_H
        #define MY_CLASS_H
        
        class MyClass {
          private:
              double *Val_; // Value data pointer
              int ValLength_; // Lenght of value data
              int Var1_;
        
          public:
                  //important: initialize the pointer to NULL:
        	  MyClass(): Var1_( 0 ), ValLength_( 0 ), Val_( 0 ) { }   
        
        	  MyClass( int len, int var1 = 0 ): Var1_( var1 ), ValLength_( len ), 
                             Val_( new double[len]) { 
        		  puts("MyClass::MyClass()");
                          //memset is not necessary
        		  memset(Val_, 0, sizeof(double)*len); //set all value to '0'
        	  }   
        
              ~MyClass() { 	
        			puts("MyClass::~MyClass()");
        			//if(Val_!=NULL) //unuseful: if (Val_ == NULL), 
                                //the 'delete[] Val_' doens't throw an error.
        			delete[] Val_; 
        	  } 
              
        	  //copy constructor is needed for 'MyClass temp = MC1';
        	  MyClass (const MyClass& mc) : Var1_ (mc.Var1_),  
                  ValLength_(mc.ValLength_), Val_(new double[mc.ValLength_]) {
        		   puts("MyClass::MyClass(const MyClass& mc)");
        		  memcpy (Val_, mc.Val_, sizeof(double) * mc.ValLength_);		
        	  }
        
        	  MyClass& operator=(const MyClass& MC) {
        	     if (this == & MC) return *this; //check this
        
        		 delete[] Val_;
        		 Val_ = new double[MC.ValLength_];			   
                 memcpy( Val_ , MC.Val_ , sizeof(double) * MC.ValLength_ );
                 ValLength_ = MC.ValLength_;
                 Var1_ = MC.Var1_;        
                 return *this;
              }
        
              // Now three methods for exchange MC1 , MC2
              friend void swap1( MyClass& MC1 , MyClass& MC2 );
              friend void swap2( MyClass& MC1 , MyClass& MC2 );
              friend void swap3( MyClass& MC1 , MyClass& MC2 );
        };
        
        
        // A funtion that swap tow integers.
        void swap_int( int& int1 , int& int2 ) {
           int temp = int1;
           int1 = int2;
           int2 = temp;
        }
        
        void swap1(MyClass& MC1 , MyClass& MC2 ) {   
          MyClass temp = MC1;
          MC1 = MC2; 
          MC2 = temp;
        }
         
        void swap2( MyClass& MC1 , MyClass& MC2 ) {  
        
          double *temp = MC1.Val_;
          MC1.Val_ = MC2.Val_;
          MC2.Val_ = temp;
          swap_int( MC1.ValLength_ , MC2.ValLength_ );
          swap_int( MC1.Var1_ , MC2.Var1_ );
        }
        
        void swap3( MyClass& MC1 , MyClass& MC2 ) {  
        	/* 
        	the default operator= won't work; what do you want?
        	Do you want MC2.Val_ points to temp memory  block? In the case, 
                when you exit from this scope temp destructor is called BUT
        	MC2 points again to undefined block of memory; when MC2 destructor 
                will called, the delete will cause a runtime error;
        	What you want to obtain doens't make sense; when there are 
                pointers inside a class operator= redefinition is necessary, not optional
        	*/
        
        	/*
            MyClass temp = MC1;
            MC1 = MC2;
            MC2 = temp;
            How can i do this? */
        }
        
        #endif MY_CLASS_H
        
        int main (int argc, char** argv) {
            MyClass MC1(10000000, 99) , MC2(5000000, 11);
            printf("swap1 Starts...\n");
            swap1( MC1 , MC2 ); 
            printf("swap2 Starts...\n");
            swap2( MC1 , MC2 );    
            return EXIT_SUCCESS;
        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by areahmath
          How can i use default assignment operator (member-by-member assignment) after overloading this operator?
          You can't. The default assignment operator is simply member by member assignment. When you write your own operator=, you have to assume responsibility for assigning all class members and not just a few of them.

          Typically, you don't need an operator= overload unless there are class members that need resource allocations or where a class member is a pointer and you don't want to just assign rthe pointer. This second case depends upon what you are doing.

          Comment

          • arashmath
            New Member
            • May 2008
            • 6

            #6
            Thanks everyone for their help.

            Comment

            Working...