Solved

Copy Constructor calling sequence

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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