| | |
Does it need a copy constructor and an operator= for a class which have array member
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
Does it need a copy constructor and an operator= for a class which have array member
0
#1 Aug 29th, 2008
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++.
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
0
#2 Aug 29th, 2008
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
Re: Does it need a copy constructor and an operator= for a class which have array member
0
#3 Aug 29th, 2008
•
•
•
•
>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.
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
Re: Does it need a copy constructor and an operator= for a class which have array mem
0
#4 Aug 29th, 2008
you can answer this yourself if you write a small test program and run it. for example:
c++ Syntax (Toggle Plain Text)
#include <iostream> struct A { A() { std::cout << "A::default constructor\n" ; } A( const A& ) { std::cout << "A::copy constructor\n" ; } A& operator= ( const A& ) { std::cout << "A::assignment\n" ; return *this ; } }; struct B { A a[2] ; }; int main() { B one ; std::cout << "-----------\n" ; B two(one) ; std::cout << "-----------\n" ; one = two ; }
Re: Does it need a copy constructor and an operator= for a class which have array member
0
#5 Aug 29th, 2008
>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.
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.
I'm here to prove you wrong.
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
Re: Does it need a copy constructor and an operator= for a class which have array mem
0
#6 Aug 29th, 2008
•
•
•
•
you can answer this yourself if you write a small test program and run it. for example:
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).
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
Re: Does it need a copy constructor and an operator= for a class which have array member
0
#7 Aug 30th, 2008
•
•
•
•
>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.
Thank you. got it.
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
Re: Does it need a copy constructor and an operator= for a class which have array member
0
#8 Nov 30th, 2008
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 .
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 .
![]() |
Similar Threads
- C++ operator overloading.....problem with compiling (C++)
- Dynamic Stack Implementation - Templated Class (C++)
- overloading operator + (C++)
- Problem declaring class object array (C++)
- dynamic array help (C++)
- undefined reference error -HELP! (C++)
- This Pointer / Copy Constructor (C++)
- Program crashing, need good debugger (C++)
- template issues - need expert debugger! (C++)
Other Threads in the C++ Forum
- Previous Thread: need help with group compiling c/c++ programs
- Next Thread: stl/map and string question
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






