473,699 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignment operator

6 New Member
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 1772
RRick
463 Recognized Expert Contributor
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
arashmath
6 New Member
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 New Member
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 Recognized Expert Moderator Expert
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
arashmath
6 New Member
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
4832
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 to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I missing something here and there is something I need to learn about? Clear, easy to understand...
16
2605
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, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
6
13379
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
1414
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, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
5
2292
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.