Avatar of Thomas Stockbruegger
Thomas Stockbruegger
Flag for Germany asked on

How to copy a class object....please take a look

Hello,
I am a beginner in Visual C++.Net. I need some help with my class.

I would like to copy one object to another one.

Here is the code:
//------------------- Test.h -------------------------------------
Class Test
{
    public:
    double AN, double BN, double CN;

    // Constructor
    Test(double a, double b, double c);

   // Destructor
    ~Test();

   //Function
   void Just_a_test();

};
//------------------- Test.cpp -------------------------------------
# include "Test.h";

Test::Test(double a, double b, double c)
{
    AN=a;
    BN=b;
    CN=c;
}

Test::~Test()
{
}

// Function
 void Just_a_test()
{
    ...just do something with AN, BN and CN
}

//----------------------------- my Dlg.cpp --------------------------------
.... on click button i.e.

double a,b,c;

a=100;
b=200;
c=300;
// create a object and call constructor
Test First_Object (a,b,c);

// call the function
First_Object.Just_a_test();

****************************************
I would like to create now a new object i.e. Second_Object and copy all the data from
First_Object

Back in c++ I just did this:
Second_Object=First_Object;

This will not work with Visual c++
Please let me know.
Please give me some credit .... I am a beginner.

Thank you
With best regards,
Thomas





Visual C++.NET

Avatar of undefined
Last Comment
Thomas Stockbruegger

8/22/2022 - Mon
AlexFM

>> Back in c++ I just did this:
Second_Object=First_Object;

This will not work with Visual c++

This works in Visual C++ exactly like in any other C++ implementation. Since your class doesn't contain operator=, C++ executes default operator which assignes all members of First_Object to second object.

Second_Object=First_Object;

This line is executed by the following way:

Second_Object.AN = First_Object.AN;
Second_Object.BN = First_Object.BN;
Second_Object.CN = First_Object.CN;

This way works unless your class contains pointers, lists or other non-trivial data which gives incorrect result by applying default operator=. In this case you need to write your own operator=, and still use the same assignment Second_Object=First_Object.
Thomas Stockbruegger

ASKER
Hi AlexFM,
thanks for your fast answer.

Yes... this works great.
//-------------------------------------------
Second_Object.AN = First_Object.AN;
Second_Object.BN = First_Object.BN;
Second_Object.CN = First_Object.CN;
//--------------------------------------------

 >> you need to write your own operator=
How can I write my own operator ?

Please take a look....like this????
 
//####################################################
//------------------- Test.h -------------------------------------
Class Test
{
    public:
    double AN, double BN, double CN;

    // Constructor
    Test(double a, double b, double c);

//- - - - - - - - - - - - - - - - - - - - - - -
    //Copy Consturctor
    Test(const Test &copy_test)
//- - - - - - - - - - - - - - - - - - - - - - -


   // Destructor
    ~Test();

   //Function
   void Just_a_test();

};
//------------------- Test.cpp -------------------------------------
# include "Test.h";

Test::Test(double a, double b, double c)
{
    AN=a;
    BN=b;
    CN=c;
}
//- - - - - - - - - - - - - - - - - - - - - - -
Test:Test(const Test &copy_test)
{
     AN=copy_test.AN
     BN=copy_test.BN
     CN=copy_test.CN
}
//- - - - - - - - - - - - - - - - - - - - - - -



Test::~Test()
{
}

// Function
 void Just_a_test()
{
    ...just do something with AN, BN and CN
}




//####################################################
Please let me know. How can I select/start  the Copy Consturctor from my Dlg.cpp ?
I very appreciate your help.
Best regards,
Thomas
AlexFM

Test& operator=(const Test& copy_test);   // h-file

// implementation:
Test& Test::operator=(const Test &copy_test)
{
     AN=copy_test.AN;
     BN=copy_test.BN;
     CN=copy_test.CN;

     return *this;
}

For Test class, operator= works like default operator=, you don't need it. But if class contains some non-trivial data (like pointers), it is necessary to write operator=.
Operator= returns Test& to compile multiple assignments, for example:
Test t1, t2, t3;

t1 = t2 = t3;    // this line is not compiled if operator= returns void

Usually operator= and copy constructor have the same code, which can be moved to another function called from both of them. But they are called in different situations.

Test t1;
Test t2(t1);   // copy constructor is called for t2

t2 = t1;       // operator= is called for t1
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
AlexFM

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Thomas Stockbruegger

ASKER
Hi Alex,
wow...thanks for your super fast answer.
I will try this....and let you know...tomorrow.

Thank you very much.
I very appreciate your help.

Have a great day.
Best regards,
Thomas

Thomas Stockbruegger

ASKER
Hi AlexFM,
I have tried your code...yes it works.
Thank you very much for your detailed information.
I now understand a copy constructor and operator=
I very appreciated you help.

Keep up the good work at experts exchange !!!
I have just added 500 points with grade A.
Thank you Alex.

Best regards,
Thomas
tsp2002