473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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.
May 2 '08 #1
5 1752
RRick
463 Expert 256MB
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?
May 2 '08 #2
I have code like this:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <mem.h>
  4.  
  5. void swap_int( int& int1 , int& int2 ) // A funtion that swap tow integers.
  6. {
  7.    int temp = int1;
  8.    int1 = int2;
  9.    int2 = temp;
  10. }
  11.  
  12. class MyClass
  13. {
  14.   private:
  15.       double *Val_; // Value data pointer
  16.       int ValLength_; // Lenght of value data
  17.       int Var1_;
  18.       int Var2_;
  19.       int Var3_; // and may be more declaration
  20.   public:
  21.       MyClass() { Val_ = NULL; ValLength_ = 0; }  // Default constructor
  22.       MyClass( int Len ) { Val_ = new double[Len]; ValLength_ = Len; }
  23.       ~MyClass() { if(Val_!=NULL) delete[] Val_; } // Free memory
  24.  
  25.       MyClass& operator=(const MyClass& MC) // Here i overwrite operator =
  26.       {
  27.          if(Val_!=NULL) delete[] Val_;
  28.          Val_ = new double[MC.ValLength_];
  29.          memcpy( Val_ , MC.Val_ , sizeof(double)*MC.ValLength_ );
  30.          ValLength_ = MC.ValLength_;
  31.          Var1_ = MC.Var1_;
  32.          Var2_ = MC.Var2_;
  33.          Var3_ = MC.Var3_;
  34.          return *this;
  35.       }
  36.       // Now three methods for exchange MC1 , MC2
  37.       friend void swap1( MyClass& MC1 , MyClass& MC2 );
  38.       friend void swap2( MyClass& MC1 , MyClass& MC2 );
  39.       friend void swap3( MyClass& MC1 , MyClass& MC2 );
  40. };
  41.  
  42. void swap1( MyClass& MC1 , MyClass& MC2 )
  43. {  /* This method performance is slow because it use overwritten version
  44.        for assignment operator */
  45.   MyClass temp = MC1;
  46.   MC1 = MC2;  
  47.   MC2 = temp;
  48. }
  49.  
  50. void swap2( MyClass& MC1 , MyClass& MC2 )
  51. {  // This one is much faster than swap1 and its result is same as swap1.
  52.   double *temp = MC1.Val_;
  53.   MC1.Val_ = MC2.Val_;
  54.   MC2.Val_ = temp;
  55.   swap_int( MC1.ValLength_ , MC2.ValLength_ );
  56.   swap_int( MC1.Var1_ , MC2.Var1_ );
  57.   swap_int( MC1.Var2_ , MC2.Var2_ );
  58.   swap_int( MC1.Var3_ , MC2.Var3_ );
  59. }
  60. void swap3( MyClass& MC1 , MyClass& MC2 )
  61. {  /* Here i try improve swap2.
  62.       if there was no user-defined operator =, the operator = would define by
  63.       default as a member-by-member assignment of the members of class.
  64.       But in MyClass operator = is overwritten.
  65.       I want compliler use default version assignment in these codes:
  66.  
  67.     MyClass temp = MC1;
  68.     MC1 = MC2;
  69.     MC2 = temp;
  70.  
  71.       How can i do this? */
  72. }
  73.  
  74. int main()
  75. {
  76.     MyClass MC1(10000000) , MC2(5000000);
  77.     printf("swap1 Starts...\n");
  78.     swap1( MC1 , MC2 );
  79.     printf("swap2 Starts...\n");
  80.     swap2( MC1 , MC2 );
  81.     printf("swap3 Starts...\n");
  82.     swap3( MC1 , MC2 );
  83.      printf("Done!\n");
  84.                 getch();
  85.     return 0;
  86. }
I explain the problem in line 61.

Thanks. :)
May 3 '08 #3
mickey0
142 100+
This maybe will help you; But there were some errors:
Expand|Select|Wrap|Line Numbers
  1. #ifndef MY_CLASS_H
  2. #define MY_CLASS_H
  3.  
  4. class MyClass {
  5.   private:
  6.       double *Val_; // Value data pointer
  7.       int ValLength_; // Lenght of value data
  8.       int Var1_;
  9.  
  10.   public:
  11.           //important: initialize the pointer to NULL:
  12.       MyClass(): Var1_( 0 ), ValLength_( 0 ), Val_( 0 ) { }   
  13.  
  14.       MyClass( int len, int var1 = 0 ): Var1_( var1 ), ValLength_( len ), 
  15.                      Val_( new double[len]) { 
  16.           puts("MyClass::MyClass()");
  17.                   //memset is not necessary
  18.           memset(Val_, 0, sizeof(double)*len); //set all value to '0'
  19.       }   
  20.  
  21.       ~MyClass() {     
  22.             puts("MyClass::~MyClass()");
  23.             //if(Val_!=NULL) //unuseful: if (Val_ == NULL), 
  24.                         //the 'delete[] Val_' doens't throw an error.
  25.             delete[] Val_; 
  26.       } 
  27.  
  28.       //copy constructor is needed for 'MyClass temp = MC1';
  29.       MyClass (const MyClass& mc) : Var1_ (mc.Var1_),  
  30.           ValLength_(mc.ValLength_), Val_(new double[mc.ValLength_]) {
  31.            puts("MyClass::MyClass(const MyClass& mc)");
  32.           memcpy (Val_, mc.Val_, sizeof(double) * mc.ValLength_);        
  33.       }
  34.  
  35.       MyClass& operator=(const MyClass& MC) {
  36.          if (this == & MC) return *this; //check this
  37.  
  38.          delete[] Val_;
  39.          Val_ = new double[MC.ValLength_];               
  40.          memcpy( Val_ , MC.Val_ , sizeof(double) * MC.ValLength_ );
  41.          ValLength_ = MC.ValLength_;
  42.          Var1_ = MC.Var1_;        
  43.          return *this;
  44.       }
  45.  
  46.       // Now three methods for exchange MC1 , MC2
  47.       friend void swap1( MyClass& MC1 , MyClass& MC2 );
  48.       friend void swap2( MyClass& MC1 , MyClass& MC2 );
  49.       friend void swap3( MyClass& MC1 , MyClass& MC2 );
  50. };
  51.  
  52.  
  53. // A funtion that swap tow integers.
  54. void swap_int( int& int1 , int& int2 ) {
  55.    int temp = int1;
  56.    int1 = int2;
  57.    int2 = temp;
  58. }
  59.  
  60. void swap1(MyClass& MC1 , MyClass& MC2 ) {   
  61.   MyClass temp = MC1;
  62.   MC1 = MC2; 
  63.   MC2 = temp;
  64. }
  65.  
  66. void swap2( MyClass& MC1 , MyClass& MC2 ) {  
  67.  
  68.   double *temp = MC1.Val_;
  69.   MC1.Val_ = MC2.Val_;
  70.   MC2.Val_ = temp;
  71.   swap_int( MC1.ValLength_ , MC2.ValLength_ );
  72.   swap_int( MC1.Var1_ , MC2.Var1_ );
  73. }
  74.  
  75. void swap3( MyClass& MC1 , MyClass& MC2 ) {  
  76.     /* 
  77.     the default operator= won't work; what do you want?
  78.     Do you want MC2.Val_ points to temp memory  block? In the case, 
  79.         when you exit from this scope temp destructor is called BUT
  80.     MC2 points again to undefined block of memory; when MC2 destructor 
  81.         will called, the delete will cause a runtime error;
  82.     What you want to obtain doens't make sense; when there are 
  83.         pointers inside a class operator= redefinition is necessary, not optional
  84.     */
  85.  
  86.     /*
  87.     MyClass temp = MC1;
  88.     MC1 = MC2;
  89.     MC2 = temp;
  90.     How can i do this? */
  91. }
  92.  
  93. #endif MY_CLASS_H
  94.  
  95. int main (int argc, char** argv) {
  96.     MyClass MC1(10000000, 99) , MC2(5000000, 11);
  97.     printf("swap1 Starts...\n");
  98.     swap1( MC1 , MC2 ); 
  99.     printf("swap2 Starts...\n");
  100.     swap2( MC1 , MC2 );    
  101.     return EXIT_SUCCESS;
  102. }
  103.  
May 4 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
May 5 '08 #5
Thanks everyone for their help.
May 7 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
20
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.