473,242 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 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 2556
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.