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.
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?
I have code like this: - #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. :)
This maybe will help you; But there were some errors: -
#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;
-
}
-
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.
Thanks everyone for their help.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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;
};
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |