![]() |
| ||
| Does it need a copy constructor and an operator= for a class which have array member Does it need a copy constructor and an operator= for a class which have array member? for example: Class A { private: B data_[100]; //B is a class }; does class A need a copy constructor and an operator? in other words, is the default copy constructor is OK for class A? I think if the default copy constructor/operator= can invoke copy constructor/operator= of class B, it will be OK, it's not necessary to define a copy constructor and operator=. otherwise, it's necessary. I used Dev-C++ to test this case, I found the operator= of class B was invoked. I don't the other compilers are the same as Dev-C++. Or this is a standard of C++. |
| ||
| Re: Does it need a copy constructor and an operator= for a class which have array member >Does it need a copy constructor and an operator= for a class which have array member? No, the default behavior will do what you want. |
| ||
| Re: Does it need a copy constructor and an operator= for a class which have array member Quote:
In my example, Do you mean the default behavior is call the operator= of class B? If the default behavior is just memory copy and class B have pointer member, it will produce a undefined result. |
| ||
| Re: Does it need a copy constructor and an operator= for a class which have array mem you can answer this yourself if you write a small test program and run it. for example: #include <iostream> |
| ||
| Re: Does it need a copy constructor and an operator= for a class which have array member >In my example, Do you mean the default behavior is call the operator= of class B? I fail to see how my reply was ambiguous. You want a member array to be properly copied even in the case where a non-default copy constructor or assignment operator exists for the array type, and that's what happens. Therefore, saying that the default behavior will do what you want is a complete answer to your question. >If the default behavior is just memory copy and class B >have pointer member, it will produce a undefined result. That's strictly a quality of implementation issue for class B. If B implements a copy constructor/assignment operator then A will have to call it when copying any object of B, even if the object of B is a member of an array. |
| ||
| Re: Does it need a copy constructor and an operator= for a class which have array mem Quote:
In fact, I have written test program and have run a lot of test case, all those case were OK. I just want that is a standard of C++ or just dependent on the compiler( I used Dev-C++ to compile my code). |
| ||
| Re: Does it need a copy constructor and an operator= for a class which have array member Quote:
Thank you. got it. |
| ||
| Re: Does it need a copy constructor and an operator= for a class which have array member The use of copy constructor and overloading an assignment operator is same ( i.e. to eliminate the data loss,specially when the class has pointer members , when objects are being copied either as an argument to fuction or return from a function or initialization or copying ) ; but both have different scenarios in which they solve this problem . A copy constrcutor is used in [1] initialisation [2] passing object as function argument [3] return from a function while assignment operator will be used in cases of explicit assignment statements. So you should have both a copy constructor and an operator= overloaded . |
| All times are GMT -4. The time now is 7:00 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC