?
Solved

Copy Constructor calling sequence

Posted on 2001-07-04
5
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

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

Accepted Solution

by:
jasonclarke earned 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

765 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