473,473 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Assignment operator=

Hi,

how do I define an assignment operator which is supposed to copy
all member attributes of one object to another where both objects are
given as pointers?

Example:

CLASS_A *source = new CLASS_A;
....
CLASS_A *dst = new CLASS_A;
dst = source;

I want that all attributes of object "source" are also assigned to object
"dst".

My idea was to define the operator in the header file of class CLASS_A:

class CLASS_A
{
public:
void operator=( const CLASS_A & );
...
private:
int a;
...
}

And in the source code:

void CLASS_A::operator=( const CLASS_A &dst )
{
a = dst.a;
}

However, this doesn't work since the operator is never invoked.

What did I wrong?

Thank you
Chris
Nov 25 '05 #1
10 2565
Christian Christmann <pl*****@yahoo.de> wrote:
Hi,

how do I define an assignment operator which is supposed to copy
all member attributes of one object to another where both objects are
given as pointers?

Example:

CLASS_A *source = new CLASS_A;
....
CLASS_A *dst = new CLASS_A;
dst = source;

I want that all attributes of object "source" are also assigned to
object "dst".
You cannot do that.
My idea was to define the operator in the header file of class [..] However, this doesn't work since the operator is never invoked.

What did I wrong?

I would not call it wrong, but you are expecting the impossible. You
cannot redefine how basic pointer assignments work. The operator= of
yours is fine. For 'source' and 'dst' you would write:

*dst = *source;

But I guess that is not new to you. What you can do, tho:

void CLASS_A::operator= (CLASS_A const* dst)
{
a = dst->a;
}

and use it like that:

*dst = source;

Another alternative would be to use your own smart pointer class.
Then you can implement a deep-copy for the smart pointers, when you
assign one to another.

hth
--
jb

(reply address in rot13, unscramble first)
Nov 25 '05 #2
Christian Christmann wrote:
Hi,

how do I define an assignment operator which is supposed to copy
all member attributes of one object to another where both objects are
given as pointers?


You can't. Pointers of any type are considered primitives by the language,
and you can't write an operator overload where all participating objects
are primitives.
Assigning one pointer to the other will always transfer the address, never
the pointee.

You could write a smart pointer that exhibits the behaviour you'd like, but
bear in mind that this violates the semantics of a smart pointer (acting
like a pointer) and the principle of least surprise (people don't expect
pointer assignment to copy the pointee).

Sebastian Redl
Nov 25 '05 #3

Christian Christmann wrote:

Example:

CLASS_A *source = new CLASS_A;
....
CLASS_A *dst = new CLASS_A;
dst = source;

I want that all attributes of object "source" are also assigned to object
"dst".

My idea was to define the operator in the header file of class CLASS_A:

class CLASS_A
{
public:
void operator=( const CLASS_A & );
This is declaration, not the definition.
...
private:
int a;
...
}

And in the source code:

void CLASS_A::operator=( const CLASS_A &dst ) // Better use the name as "source", since assignment is usually done
from "source" to "destination".

{
a = dst.a;
}

However, this doesn't work since the operator is never invoked.
operator= will be invoked when you will say something like
*dst = *source;
What did I wrong?
If you are saying
dst = source
then its a case of memory leak.

Thank you
Chris


Nov 25 '05 #4
* Christian Christmann:

how do I define an assignment operator which is supposed to copy
all member attributes of one object to another where both objects are
given as pointers?
You don't have to: the compiler does that automatically for you.

Example:

CLASS_A *source = new CLASS_A;
....
CLASS_A *dst = new CLASS_A;
dst = source;

I want that all attributes of object "source" are also assigned to object
"dst".
*dst = *source;

Wen there are derived classes involved, this may involve slicing.

In that case you should prohibit assignment (by declaring a private
assignment operator) rather than trying to implement polymorphic
assignment.

My idea was to define the operator in the header file of class CLASS_A:

class CLASS_A
Don't use all uppercase names except for macros.

{
public:
void operator=( const CLASS_A & );
The result type for an assignment operator should ordinarily be
CLASS_A&.

...
private:
int a;
...
}
Missing semicolon.
And in the source code:

void CLASS_A::operator=( const CLASS_A &dst )
The argument is not the destination, but the source.

By the way, it's generally not a good idea to use arbitrarily shortened
names.

{
a = dst.a;
}
That would be a very reverse kind of assignment.
However, this doesn't work since the operator is never invoked.

What did I wrong?


Generally, see above.

For the lack of invocation you don't show any code doing the invocation,
and so nothing can be said about that last question.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 25 '05 #5
Christian Christmann wrote:
Hi,

how do I define an assignment operator which is supposed to copy
all member attributes of one object to another where both objects are
given as pointers?
You can't. If you assign a pointer to another pointer then you are
assigning pointers not objects.
Example:

CLASS_A *source = new CLASS_A;
....
CLASS_A *dst = new CLASS_A;
dst = source;

I want that all attributes of object "source" are also assigned to object
"dst".
*dst = *source;

Now both expressions in the assignment are of type CLASS_A so the
assignment operator for CLASS_A is called. If you don't want the
default compiler-generated behaviour for that (member-wise assignment)
you can write your own assignment operator.

CLASS_A source;
CLASS_A dst;
dst = source;

would also achieve want you want, but without the complication of
pointers. Is there a reason you are dynamically allocating your
objects?
My idea was to define the operator in the header file of class CLASS_A:

class CLASS_A
{
public:
void operator=( const CLASS_A & );
should be

CLASS_A& operator=( const CLASS_A & );

to allow chaining assignments, as in

dst1 = dst2 = source;
...
private:
int a;
...
}

And in the source code:

void CLASS_A::operator=( const CLASS_A &dst )
CLASS_A& operator=( const CLASS_A & );
{
a = dst.a; return *this; }
That is how you define your own assignment operator. But since all it
is doing is member-wise assignment, there is no need to define your
own. The compiler-generated one will do just fine.
However, this doesn't work since the operator is never invoked.

What did I wrong?


See above.

Also, note that CLASS_A isn't a very good class name. It is a common
practice to reserve names in all uppercase for preprocessor macro
names. If you always use all uppercase for macros and never use all
uppercase in your code, you can remove the risk of the preprocessor
silently changing your code.

Gavin Deane

Nov 25 '05 #6
u can make ur code correct by using
*dst=*source

Nov 25 '05 #7
On Fri, 25 Nov 2005 13:15:46 +0100, Christian Christmann
<pl*****@yahoo.de> wrote:
Hi,

how do I define an assignment operator which is supposed to copy
all member attributes of one object to another where both objects are
given as pointers?

Example:

CLASS_A *source = new CLASS_A;
....
CLASS_A *dst = new CLASS_A;
dst = source;

I want that all attributes of object "source" are also assigned to object
"dst".

My idea was to define the operator in the header file of class CLASS_A:

class CLASS_A
{
public:
void operator=( const CLASS_A & );
...
private:
int a;
...
}

And in the source code:

void CLASS_A::operator=( const CLASS_A &dst )
{
a = dst.a;
}

However, this doesn't work since the operator is never invoked.

What did I wrong?

Thank you
Chris


It is not possiblñe the way you are trying to do it, as you are not
trying to overload a class, but a pointer.

The syntax should be:
*dst=*source;
for the copy operator to be invoked.

Best regards,

-- Zara

Nov 25 '05 #8

Christian Christmann wrote:
Hi,

how do I define an assignment operator which is supposed to copy
all member attributes of one object to another where both objects are
given as pointers?

Example:

CLASS_A *source = new CLASS_A;
....
CLASS_A *dst = new CLASS_A;
dst = source;

I want that all attributes of object "source" are also assigned to object
"dst".


Override pointer behavior by creating a pointer class and use it to
manage your pointers. Something like this might work:

class Ptr {
CLASS_A *ptr;
public:
....
Ptr& operator=(const Ptr& cpy) { *ptr = *cpy; return *this; }
};

assuming you have overridden * and various other things.

Nov 25 '05 #9
|| Don't use all uppercase names except for macros.
Alf, could you elaborate on this? I tend to agree but I'm not sure if
this is just a stylistic issue.

Thanks

Nov 26 '05 #10
* ma740988:
|| Don't use all uppercase names except for macros.
Alf, could you elaborate on this? I tend to agree but I'm not sure if
this is just a stylistic issue.


Otherwise it's much more likely that macros conflict with other names.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 26 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
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,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
9
by: Matthew Polder | last post by:
Hi, When a class Apple is written and the assignment operator is not explicitly declared, the operator Apple& operator=(const Apple&) is created by the compiler. Is there any difference...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
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; };
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
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...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
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
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,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.