Solved

Copy Constructor calling sequence

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

867 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

22 Experts available now in Live!

Get 1:1 Help Now