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++.

>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.

>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.

Thank you for your reply.

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.

you can answer this yourself if you write a small test program and run it. for example:

#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 ;
}

>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.

you can answer this yourself if you write a small test program and run it. for example:

Thank you.

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).

>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.

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 .

This question has already been answered. Start a new discussion instead.