RSS Forums RSS

default arguments in copy constructor

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 1,044
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 62
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

default arguments in copy constructor

  #1  
Dec 3rd, 2008
If I add default arguments to the end of my copy constructor, is it still considered a copy constructor? E.g. will it still be used in the places where a copy constructor would normally be used (or automatically generated).

I can verify that it works on one compiler (g++). That is, this does output the "copy ctor" message 3 times:

  1. #include <iostream>
  2.  
  3. class Context
  4. {
  5. // real thing actually has some methods
  6. };
  7.  
  8. class A
  9. {
  10. private:
  11. Context & m_context;
  12.  
  13. public:
  14. A ( Context * context )
  15. :m_context ( MATT_DEREF ( context ) )
  16. {
  17. // probably do something with context here..
  18. }
  19.  
  20. A ( const A & a, Context * new_context = 0 )
  21. :m_context ( new_context ? *new_context : a.m_context )
  22. {
  23. std::cout << "copy ctor\n";
  24. // probably do something with context here..
  25. }
  26. };
  27.  
  28. void test ( A a )
  29. { return; }
  30.  
  31. int main ( void )
  32. {
  33. Context c1, c2;
  34. A a1 ( &c1 );
  35. // implictly copying by passing as value
  36. test ( a1 );
  37. // explicitly copying in the same context
  38. A a2 ( a1 );
  39. // copying to a new context
  40. A a3 ( a1, &c2 );
  41. return EXIT_SUCCESS;
  42. }

But, will this always work O.K.? are there any caveats/pitfalls etc?

Other alternatives considered:

- I can't have the generated copy ctor used, for various reasons (e.g. other pointers/reference members shouldn't be shallow copied ).
- I don't want to pass the pointer to "context" in later, because that makes other areas unpleasant. E.g. I need to use "context" in the constructor, and it's not meant to be trivially reseatable.
- I do want the rest of the copy constructors features (i.e. being able to initialize the object & its bases in order).
- I'd rather not have to write 3 constructors for every class like this.
Last edited by MattEvans : Dec 3rd, 2008 at 1:53 pm.
Plato forgot the nullahedron..
AddThis Social Bookmark Button
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: default arguments in copy constructor

  #2  
Dec 3rd, 2008
The C++ Standard:
A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6 ). Example: X:: X(const X& ) and X:: X(X&, int=1) are copy constructors.
Other alternatives of... what?...
Last edited by ArkM : Dec 3rd, 2008 at 7:08 pm.
Reply With Quote  
Posts: 1,044
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 62
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: default arguments in copy constructor

  #3  
Dec 3rd, 2008
Ok, thanks.

Other alternatives meaning, the list I put of things that I could have done to avoid having to do this.
Plato forgot the nullahedron..
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 536 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:33 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC