943,563 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1326
  • C++ RSS
Dec 3rd, 2008
0

default arguments in copy constructor

Expand Post »
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:

CPP Syntax (Toggle Plain Text)
  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 2:53 pm.
Similar Threads
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Dec 3rd, 2008
0

Re: default arguments in copy constructor

The C++ Standard:
Quote ...
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 8:08 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 3rd, 2008
0

Re: default arguments in copy constructor

Ok, thanks.

Other alternatives meaning, the list I put of things that I could have done to avoid having to do this.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

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: Linked lists
Next Thread in C++ Forum Timeline: 3 problems





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


Follow us on Twitter


© 2011 DaniWeb® LLC