943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1637
  • C++ RSS
Aug 29th, 2008
0

Does it need a copy constructor and an operator= for a class which have array member

Expand Post »
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++.
Similar Threads
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Aug 29th, 2008
1

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 29th, 2008
0

Re: Does it need a copy constructor and an operator= for a class which have array member

Click to Expand / Collapse  Quote originally posted by Narue ...
>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.
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Aug 29th, 2008
0

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:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. struct A
  4. {
  5. A() { std::cout << "A::default constructor\n" ; }
  6. A( const A& ) { std::cout << "A::copy constructor\n" ; }
  7. A& operator= ( const A& )
  8. { std::cout << "A::assignment\n" ; return *this ; }
  9. };
  10.  
  11. struct B { A a[2] ; };
  12.  
  13. int main()
  14. {
  15. B one ;
  16. std::cout << "-----------\n" ;
  17. B two(one) ;
  18. std::cout << "-----------\n" ;
  19. one = two ;
  20. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Aug 29th, 2008
1

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 29th, 2008
0

Re: Does it need a copy constructor and an operator= for a class which have array mem

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
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).
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Aug 30th, 2008
0

Re: Does it need a copy constructor and an operator= for a class which have array member

Click to Expand / Collapse  Quote originally posted by Narue ...
>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.
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Nov 30th, 2008
0

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 .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhijitm is offline Offline
6 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help with group compiling c/c++ programs
Next Thread in C++ Forum Timeline: stl/map and string question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC