Assignment operator=

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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::eek:perator=( 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
 
J

Jakob Bieling

Christian Christmann said:
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::eek:perator= (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
 
S

Sebastian Redl

Christian said:
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
 
N

Neelesh Bodas

Christian said:
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::eek:perator=( 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.
 
A

Alf P. Steinbach

* 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::eek:perator=( 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.
 
D

deane_gavin

Christian said:
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::eek:perator=( 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
 
Z

Zara

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::eek:perator=( 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
 
R

roberts.noah

Christian said:
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.
 
M

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.

Thanks
 
A

Alf P. Steinbach

* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,296
Messages
2,571,535
Members
48,281
Latest member
DaneLxa72

Latest Threads

Top