Solved

Copy Constructor calling sequence

Posted on 2001-07-04
5
244 Views
Last Modified: 2010-05-18
Will the default copy constructor of C++ call the
copy constructors of member variables, (which have
the copy constructor defined)

example :

class A{
public :
  A(const A &a); // copy constructor,
                 // allocates appropriately
private :
  char *_a; // default copy constructor no good!
};

class B{
  // DOES not have a copy constructor
private :
  A a;      // object (not pointer) of A
};

main()
{
  B b;
  B b1(b);
}

Question : Will A's copy constructor be called?

0
Comment
Question by:wkf
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6252608
Simple answer is yes!
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 100 total points
ID: 6252681
Slightly more info:

In a default copy constructor, If they are defined, copy constructors will be called for each data object of the class (in the order that they appear in the class).

For objects without a copy constructor, a bitwise copy will be performed.
0
 
LVL 5

Expert Comment

by:proskig
ID: 6252691
Yes, see this extract from Standard

4- If the class definition does not explicitly declare a copy constructor, one is declared implicitly. Thus, for the class definition

struct X {
     X(const X&, int);
};

a copy constructor is implicitly-declared. If the user-declared constructor is later defined as
X::X(const X& x, int i =0) { /* ... */ }

then any use of X's copy constructor is ill-formed because of the ambiguity; no diagnostic is required.
-5- The implicitly-declared copy constructor for a class X will have the form

X::X(const X&)

if
each direct or virtual base class B of X has a copy constructor whose first parameter is of type const B& or const volatile B&, and
for all the nonstatic data members of X that are of a class type M (or array thereof), each such class type has a copy constructor whose first parameter is of type const M& or const volatile M&.*
[Footnote: This implies that the reference parameter of the implicitly-declared copy constructor cannot bind to a volatile lvalue; see diff.special. --- end foonote]
Otherwise, the implicitly declared copy constructor will have the form
X::X(X&)

An implicitly-declared copy constructor is an inline public member of its class.
0
 
LVL 5

Expert Comment

by:proskig
ID: 6252697
jasonclarke: did not see your post
0
 
LVL 2

Expert Comment

by:antoinebf
ID: 6252922
... copy constructors of member variables, (which have
the copy constructor defined)
>be aware that compilers will generate a copy constructor for your class one is needed/used.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now