471,599 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 2465
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Edward Diener | last post: by
9 posts views Thread by Rick N. Backer | last post: by
9 posts views Thread by Matthew Polder | last post: by
1 post views Thread by Jon Slaughter | last post: by
6 posts views Thread by Neil Zanella | last post: by
11 posts views Thread by anongroupaccount | last post: by
9 posts views Thread by George2 | last post: by
reply views Thread by leo001 | last post: by

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.