Thomas Stockbruegger
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
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
}
//------------------------
.... 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
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 ©_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 ©_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
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 ©_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 ©_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
Test& operator=(const Test& copy_test); // h-file
// implementation:
Test& Test::operator=(const Test ©_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
// implementation:
Test& Test::operator=(const Test ©_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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
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