Solved

Copy Constructor calling sequence

Posted on 2001-07-04
5
248 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 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

Independent Software Vendors: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

730 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