Copy constructor invocation for member objects - c++

C++ says that to create a copy constructor for a class that uses composition, the compiler recursively calls the copy constructors for all the member objects. I tried that same thing in the below code:
class A
{
public:
A(){cout<<"A constructor called"<<endl;}
A(const A&){cout<<"A copy constructor called"<<endl;}
};
class B
{
public:
B(){cout<<"B constructor called"<<endl;}
B(const B&){cout<<"B copy constructor called"<<endl;}
};
class C
{
A a;
B b;
public:
C(){cout<<"C constructor called"<<endl;}
C(const C&){cout<<"C copy constructor called"<<endl;}// If you comment this line, you will get output: Case 1 (see below) and if you don't comment, you will get o/p: case 2(see below)
};
void main()
{
C c;
cout<<endl;
C c2 = c;
}`
Case 1:
A constructor called
B constructor called
C constructor called
A copy constructor called
B copy constructor called
Case 2:
A constructor called
B constructor called
C constructor called
A constructor called
B constructor called
C copy constructor called
My doubt is that the o/p for case 2 should be: A, B, C, constructor called and then.. A, B, C copy constructor called. But it is not happening. Please help.

That would happen, except that you have provided your own copy constructor for C, which tells the compiler "don't provide the default copy constructor."
If you want your copy constructor to do the same thing that the implicitly defined copy constructor would do (along with your extra printing), you'd need to define it as follows:
C(const C& other)
: a(other.a), b(other.b)
{
std::cout << "C copy constructor called" << std::endl;
}

The standard is only explaining behavior for the default copy constructor. The default copy constructor is the one you're using when you comment out the copy constructor you wrote for C. If you write your own copy constructor you must explicitly copy any members you wish to copy, like so:
C( const C& c ) : a(c.a), b(c.b) {cout<<"C copy constructor called"<<endl;}
Obivously writing your own copy constructor is somewhat error prone, so it's best to rely on the default one whenever possible.

In both cases, C::C( C const &) copy constructor is called. When it is implicit, then it calls copy constructors of base classes. When it is explicit the way you define it, it calls default constructors of A and B. This is expected behavior. Note, C c2 = c; is equal to C c2(c);, there is no temporaries.

Related

Am I calling the constructor or the copy constructor? [duplicate]

I don't understand the difference between assignment constructor and copy constructor in C++. It is like this:
class A {
public:
A() {
cout << "A::A()" << endl;
}
};
// The copy constructor
A a = b;
// The assignment constructor
A c;
c = a;
// Is it right?
I want to know how to allocate memory of the assignment constructor and copy constructor?
A copy constructor is used to initialize a previously uninitialized object from some other object's data.
A(const A& rhs) : data_(rhs.data_) {}
For example:
A aa;
A a = aa; //copy constructor
An assignment operator is used to replace the data of a previously initialized object with some other object's data.
A& operator=(const A& rhs) {data_ = rhs.data_; return *this;}
For example:
A aa;
A a;
a = aa; // assignment operator
You could replace copy construction by default construction plus assignment, but that would be less efficient.
(As a side note: My implementations above are exactly the ones the compiler grants you for free, so it would not make much sense to implement them manually. If you have one of these two, it's likely that you are manually managing some resource. In that case, per The Rule of Three, you'll very likely also need the other one plus a destructor.)
The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it’s really not all that difficult. Summarizing:
If a new object has to be created before the copying can occur, the copy constructor is used.
If a new object does not have to be created before the copying can occur, the assignment operator is used.
Example for assignment operator:
Base obj1(5); //calls Base class constructor
Base obj2; //calls Base class default constructor
obj2 = obj1; //calls assignment operator
Example for copy constructor:
Base obj1(5);
Base obj2 = obj1; //calls copy constructor
The first is copy initialization, the second is just assignment. There's no such thing as assignment constructor.
A aa=bb;
uses the compiler-generated copy constructor.
A cc;
cc=aa;
uses the default constructor to construct cc, and then the *assignment operator** (operator =) on an already existing object.
I want know how to allocate memory of the assignment constructor and copy constructor?
IDK what you mean by allocate memory in this case, but if you want to see what happens, you can:
class A
{
public :
A(){ cout<<"default constructor"<<endl;};
A(const A& other){ cout<<"copy constructor"<<endl;};
A& operator = (const A& other){cout <<"assignment operator"<<endl;}
};
I also recommend you take a look at:
Why is copy constructor called instead of conversion constructor?
What is The Rule of Three?
In a simple words,
Copy constructor is called when a new object is created from an existing object, as a copy of the existing object.
And assignment operator is called when an already initialized object is assigned a new value from another existing object.
Example-
t2 = t1; // calls assignment operator, same as "t2.operator=(t1);"
Test t3 = t1; // calls copy constructor, same as "Test t3(t1);"
What #Luchian Grigore Said is implemented like this
class A
{
public :
int a;
A(){ cout<<"default constructor"<<endl;};
A(const A& other){ cout<<"copy constructor"<<endl;};
A& operator = (const A& other){cout <<"assignment operator"<<endl;}
};
void main()
{
A sampleObj; //Calls default constructor
sampleObj.a = 10;
A copyConsObj = sampleObj; //Initializing calls copy constructor
A assignOpObj; //Calls default constrcutor
assignOpObj = sampleObj; //Object Created before so it calls assignment operator
}
OUTPUT
default constructor
copy constructor
default constructor
assignment operator
the difference between a copy constructor and an assignment constructor is:
In case of a copy constructor it creates a new object.(<classname> <o1>=<o2>)
In case of an assignment constructor it will not create any object means it apply on already created objects(<o1>=<o2>).
And the basic functionalities in both are same, they will copy the data from o2 to o1 member-by-member.
I want to add one more point on this topic.
"The operator function of assignment operator should be written only as a member function of the class." We can't make it as friend function unlike other binary or unary operator.
Something to add about copy constructor:
When passing an object by value, it will use copy constructor
When an object is returned from a function by value, it will use copy constructor
When initializing an object using the values of another object(as the example you give).

is it possible to forbid assignment if there is no explicit copy constructor? c++

Let's assume I have class with no explicit copy constructor. Is it possible to forbid operation of assigning or copying objects for this class? For example:
class A
{
// data, methods, but no copy constructor and no overloaded assignment operator
};
A object1;
A object2;
object1 = object2; // make compiler error here
A object3 = object1; // or here
You could mark the copy-constructor and copy-assignment operator as deleted:
class A
{
public:
...
A(const A&) = delete;
A& operator=(const A&) = delete;
};
If your compiler doesn't support C++11 features like this, just make the functions private.
Try this
private:
A(A const&); //Don't implement it
A& operator=(A const&);//Don't implement it
or with C++ 11
A(A const&) = delete;
A& operator=(A const&) = delete;
Let's assume I have class with no explicit copy constructor. Is it possible to forbid operation of assigning or copying objects for this class?
Derive it from boost::noncopyable, a base class that is non-copyable, or add a non-static data member that is non-copyable.
If you dont have explicit copy constructor. compiler will create a default one for you.
To forbid copying make copy constructor private.
To forbid assignment make assignment operator private
I also can declare copy constructor or assignment operator without definition. When I call either of them I get linker error.
The implicit copy constructor and assignment operator can only be called if the compiler can generate them. They (conceptually, at least) copy the base class, and then perform memberwise copy of all members. If either of those operations cannot be performed, no implicit copy constructor or assignment operator is generated.
Therefore, you can inhibit these operations by deriving from a non-copyable base class or by including a (non-static) member of a non-copyable type.
As a coding principle, I think it's clearer to explicitly declare the operations as deleted (or private, pre-C++11) members, rather than relying on the presence of a non-copyable member or base class. I'll make an exception if the member/base is named to convey that intent (e.g. boost::noncopyable).
Your question has two part:
First is Explicit constructor, the purpose of explicit constructor is to avoid the implicit construction. By implicit construction I mean
struct C {
int a;
C(int p):a(p) {}
~C(){std::cout<<"Destructor for "<<a<<'\n';}
};
int main() {
C c1 = 142; // implicit-initialization, calls C::C(42)
C c2{12}; //
return 0;
}
But if we make constructor explicit as
explicit C(int a);
Then following code will not compile.
C c1 = 142; // would not compile
This was about the explicit constructor.
Second part is avoiding copy and assignment. So, for that you have two options
As suggested by others,use boost::noncopyable
Remember "The rule of three/five/zero"
http://en.cppreference.com/w/cpp/language/rule_of_three, make your
copy ctor, move ctor & assignment operator as deleted.

Copy constructor when declaring two objects

Does declaring the following:
MyClass myFirstObject;
MyClass mySecondObject = myFirstObject;
mean that the copy constructor is used for the second object even if no parameter is passed?
Yes,A copy constructor is used to initialize an object with a differnt object of same type.
Situation where a copy constructor is called
MyClass A;
MyClass B(A); //Explicit Copy constructor invoked
MyClass C = A; //Implicit Copy constructor invoked
An easy way to check is by adding a cout statement in the copy constructor:
MyClass::MyClass(const MyClass&){
std::cout << "I am called!";
/*do stuff*/
}

Why isnt the copy constructor of member class called?

class member
{
public:
member()
{
cout<<"Calling member constr"<<'\n';
}
member(const member&)
{
cout<<"Calling member copy constr"<<'\n';
}
};
class fred
{
public:
fred()
{
cout<<"calling fred constr"<<'\n';
}
fred(const fred &)
{
cout<<"Calling fred copy constr"<<'\n';
}
protected:
member member_;
};
int main()
{
fred a;
fred b=a;
}
Output:
Calling member constr
calling fred constr
**Calling member constr**
Calling fred copy constr
Because you did not call member's copy constructor. You need to copy the members explicitly if you override the default copy constructor of fred.
fred(const fred& other) : member_(other.member_) {
cout<<"Calling fred copy constr"<<'\n';
}
It isn't called because you explicitly asked the compiler not to call it. When you defined your own copy constructor for class fred, you essentially told the compiler that you wanted to take matters into your hands and do the copying yourself. Since you do nothing to copy the member_ in the fred's copy constructor, it isn't copied.
If you get rid of the explicit definition of fred's copy constructor, the compiler will provide an implicit one for you, which will call the member's copy constructor to copy member_.
If you insist on defining fred's copy constructor yourself, you have to copy member_ yourself, as KennyTM suggested.
If class A has a class B member and you explicitly implement the copy ctor for class A, then you must explicitly copy the members there, in this case call the B copy ctor, otherwise the B member will be default constructed, not copy constructed.
You have to explicitly call it in this case. Because, you have overloaded constructors.
Btw, When I see "Fred", it reminds me this useful resource, please have look for further understanding of C++ constructors: http://www.parashift.com/c++-faq-lite/ctors.html

C++ implicit copy constructor for a class that contains other objects

I know that the compiler sometimes provides a default copy constructor if you don't implement yourself. I am confused about what exactly this constructor does. If I have a class that contains other objects, none of which have a declared copy constructor, what will the behavior be? For example, a class like this:
class Foo {
Bar bar;
};
class Bar {
int i;
Baz baz;
};
class Baz {
int j;
};
Now if I do this:
Foo f1;
Foo f2(f1);
What will the default copy constructor do? Will the compiler-generated copy constructor in Foo call the compiler-generated constructor in Bar to copy over bar, which will then call the compiler-generated copy constructor in Baz?
Foo f1;
Foo f2(f1);
Yes this will do what you expect it to:
The f2 copy constructor Foo::Foo(Foo const&) is called.
This copy constructs its base class and then each member (recursively)
If you define a class like this:
class X: public Y
{
private:
int m_a;
char* m_b;
Z m_c;
};
The following methods will be defined by your compiler.
Constructor (default) (2 versions)
Constructor (Copy)
Destructor (default)
Assignment operator
Constructor: Default:
There are actually two default constructors.
One is used for zero-initialization while the other is used for value-initialization. The used depends on whether you use () during initialization or not.
// Zero-Initialization compiler generated constructor
X::X()
:Y() // Calls the base constructor
// If this is compiler generated use
// the `Zero-Initialization version'
,m_a(0) // Default construction of basic PODS zeros them
,m_b(0) //
m_c() // Calls the default constructor of Z
// If this is compiler generated use
// the `Zero-Initialization version'
{
}
// Value-Initialization compiler generated constructor
X::X()
:Y() // Calls the base constructor
// If this is compiler generated use
// the `Value-Initialization version'
//,m_a() // Default construction of basic PODS does nothing
//,m_b() // The values are un-initialized.
m_c() // Calls the default constructor of Z
// If this is compiler generated use
// the `Value-Initialization version'
{
}
Notes: If the base class or any members do not have a valid visible default constructor then the default constructor can not be generated. This is not an error unless your code tries to use the default constructor (then only a compile time error).
Constructor (Copy)
X::X(X const& copy)
:Y(copy) // Calls the base copy constructor
,m_a(copy.m_a) // Calls each members copy constructor
,m_b(copy.m_b)
,m_c(copy.m_c)
{}
Notes: If the base class or any members do not have a valid visible copy constructor then the copy constructor can not be generated. This is not an error unless your code tries to use the copy constructor (then only a compile time error).
Assignment Operator
X& operator=(X const& copy)
{
Y::operator=(copy); // Calls the base assignment operator
m_a = copy.m_a; // Calls each members assignment operator
m_b = copy.m_b;
m_c = copy.m_c;
return *this;
}
Notes: If the base class or any members do not have a valid viable assignment operator then the assignment operator can not be generated. This is not an error unless your code tries to use the assignment operator (then only a compile time error).
Destructor
X::~X()
{
// First runs the destructor code
}
// This is psudo code.
// But the equiv of this code happens in every destructor
m_c.~Z(); // Calls the destructor for each member
// m_b // PODs and pointers destructors do nothing
// m_a
~Y(); // Call the base class destructor
If any constructor (including copy) is declared then the default constructor is not implemented by the compiler.
If the copy constructor is declared then the compiler will not generate one.
If the assignment operator is declared then the compiler will not generate one.
If a destructor is declared the compiler will not generate one.
Looking at your code the following copy constructors are generated:
Foo::Foo(Foo const& copy)
:bar(copy.bar)
{}
Bar::Bar(Bar const& copy)
:i(copy.i)
,baz(copy.baz)
{}
Baz::Baz(Baz const& copy)
:j(copy.j)
{}
The compiler provides a copy constructor unless you declare (note: not define) one yourself. The compiler-generated copy constructor simply calls the copy constructor of each member of the class (and of each base class).
The very same is true for the assignment operator and the destructor, BTW. It is different for the default constructor, though: That is provided by the compiler only if you do not declare any other constructor yourself.
Yes, the compiler-generated copy constructor performs a member-wise copy, in the order in which the members are declared in the containing class. If any of the member types do not themselves offer a copy constructor, the would-be copy constructor of the containing class cannot be generated. It may still be possible to write one manually, if you can decide on some appropriate means to initialize the value of the member that can't be copy-constructed -- perhaps by using one of its other constructors.
The C++ default copy constructor creates a shallow copy. A shallow copy will not create new copies of objects that your original object may reference; the old and new objects will simply contain distinct pointers to the same memory location.
The compiler will generate the needed constructors for you.
However, as soon as you define a copy-constructor yourself, the compiler gives up generating anything for that class and will give and error if you don't have the appropriate constructors defined.
Using your example:
class Baz {
Baz(const Baz& b) {}
int j;
};
class Bar {
int i;
Baz baz;
};
class Foo {
Bar bar;
};
Trying to default instantiate or copy-construct Foo will throw an error since Baz is not copy-constructable and the compiler can't generate the default and copy constroctor for Foo.