According to the explanation in C++ primer 5th.
Initiate a string with char array and using =
operator. It will actually do below two things:
1: Call constructor which accept a const char *
to create a temporary string object
.
2: Call copy constructor to initiate the true variable;
chapter 13.1 page 618
string null_book = "9-999-99999-9"; // copy initialization
I made a test. and it seems that when I initiate A object with a cahr array. copy construtor have never been called.
#include <iostream>
int b =5;
using namespace std;
class A
{
public:
A(const char * ch) :chr(*ch) {cout << "contruct ";};
A(const A & a) : chr(0) {cout << "copy_construc ";::b = 2;} ;
A &operator=(const A & a) {cout << "assignment"; return *this;};
char chr;
};
int main() {
A a = "qweqeasd";
cout << b;
cout << a.chr;
A c = A("wrwsx");
cout << b;
cout << c.chr;
}
output:
contruct 5qcontruct 5w