Medicine
Medicine

Reputation: 2023

c++ user defined member in copy constructor

class A
{
std::string name;
public:
A(const A & rhs)
{
name = rhs.name;
}
};

In copy constructor of class A above, will the assignment operator of string class is called or copy constructor of string class?

name data member is not defined yet, so wouldn't the copy constructor be called?

Upvotes: 0

Views: 110

Answers (2)

YePhIcK
YePhIcK

Reputation: 5856

A default (compiler-generated) assignment operator will be called that does a member-by-member assignment

Upvotes: 0

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

Assignment operator. If you want copy constructor:

A(const A& rhs)
: name(rhs.name)
{
}

Upvotes: 4

Related Questions