DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Does it need a copy constructor and an operator= for a class which have array member (http://www.daniweb.com/forums/thread142944.html)

littlestone Aug 29th, 2008 6:31 am
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++.

Narue Aug 29th, 2008 10:15 am
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.

littlestone Aug 29th, 2008 1:22 pm
Re: Does it need a copy constructor and an operator= for a class which have array member
 
Quote:

Originally Posted by Narue (Post 680660)
>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.

vijayan121 Aug 29th, 2008 1:31 pm
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>

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

Narue Aug 29th, 2008 2:08 pm
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.

littlestone Aug 29th, 2008 11:59 pm
Re: Does it need a copy constructor and an operator= for a class which have array mem
 
Quote:

Originally Posted by vijayan121 (Post 680802)
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).

littlestone Aug 30th, 2008 12:03 am
Re: Does it need a copy constructor and an operator= for a class which have array member
 
Quote:

Originally Posted by Narue (Post 680830)
>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.

abhijitm Nov 30th, 2008 2:33 pm
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