help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Assignment operator


From: Maett
Subject: Re: Assignment operator
Date: Fri, 25 Nov 2005 14:19:52 +0100
User-agent: Opera M2/7.54 (Win32, build 3929)

Christian Christmann <plfriko@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;

This assigns the *pointers*. What you want is to copy the objects the pointers 
point to.
You must dereference the pointers.

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?


Write
        *dst = *source;
Now your operator= should be invoked.

Thank you


Chris




Cheers
Maett


reply via email to

[Prev in Thread] Current Thread [Next in Thread]