A a2 = a1; no call to operator =?

M

moleskyca1

I cannot figure out why line A a2 = a1; won't call my operator =()?

Program prints:
In A(const char *str)

nothing more! Here is code:
class A {
public:
char *s;
public:
A(): s(NULL) { cout << "In A()" << endl;}
A(const char *str) { cout << "In A(const char *str)" << endl; s = new
char[strlen(str)]; strcpy(s, str); }
A &operator =(const A &rhs);

//~A() { if (s) delete[] s; }
};
A &A::eek:perator =(const A &rhs) {
cout << "in A &operator =(const A &rhs)" << endl;
int size = strlen(rhs.s);
s = new char[size];
strcpy(s, rhs.s);
return *this; }

int main() {
A a1("ABCDE");
A a2 = a1;
return 0;
}
 
P

Pierre Barbier de Reuille

I cannot figure out why line A a2 = a1; won't call my operator =()?

Because it is another syntax for:

A a2(a1);

Pierre
Program prints:
In A(const char *str)

nothing more! Here is code:
class A {
public:
char *s;
public:
A(): s(NULL) { cout << "In A()" << endl;}
A(const char *str) { cout << "In A(const char *str)" << endl; s = new
char[strlen(str)]; strcpy(s, str); }
A &operator =(const A &rhs);

//~A() { if (s) delete[] s; }
};
A &A::eek:perator =(const A &rhs) {
cout << "in A &operator =(const A &rhs)" << endl;
int size = strlen(rhs.s);
s = new char[size];
strcpy(s, rhs.s);
return *this; }

int main() {
A a1("ABCDE");
A a2 = a1;
return 0;
}
 
N

Noah Roberts

I cannot figure out why line A a2 = a1; won't call my operator =()?

Because it calls your copy constructor. The one created for you by
default since you didn't make one. This means your pointer is copied
directly instead of copying the string.

This would result in double deletion if you had a destructor, as it
stands it just means you get only one memory leak instead of two.
Program prints:
In A(const char *str)

nothing more! Here is code:
class A {
public:
char *s;
public:
A(): s(NULL) { cout << "In A()" << endl;}
A(const char *str) { cout << "In A(const char *str)" << endl; s = new
char[strlen(str)]; strcpy(s, str); }
A &operator =(const A &rhs);

//~A() { if (s) delete[] s; }
};
A &A::eek:perator =(const A &rhs) {
cout << "in A &operator =(const A &rhs)" << endl;
int size = strlen(rhs.s);
s = new char[size];
strcpy(s, rhs.s);
return *this; }

int main() {
A a1("ABCDE");
A a2 = a1;
return 0;
}
 
S

sonison.james

I cannot figure out why line A a2 = a1; won't call my operator =()?

Program prints:
In A(const char *str)

nothing more! Here is code:
class A {
public:
char *s;
public:
A(): s(NULL) { cout << "In A()" << endl;}
A(const char *str) { cout << "In A(const char *str)" << endl; s = new
char[strlen(str)]; strcpy(s, str); }
A &operator =(const A &rhs);

//~A() { if (s) delete[] s; }
};
A &A::eek:perator =(const A &rhs) {
cout << "in A &operator =(const A &rhs)" << endl;
int size = strlen(rhs.s);
s = new char[size];
strcpy(s, rhs.s);
return *this; }

int main() {
A a1("ABCDE");
A a2 = a1;
return 0;
}

Because it's not supposed to. In case of initialization the compiler
will invoke the copy constructor, in your case the deafult provided by
the compiler to copy construct the new object. Assignment operator
would be invoked if you had a plain assignment as in

a2 = a1;


Thanks and regards
SJ
 
K

Kai-Uwe Bux

I cannot figure out why line A a2 = a1; won't call my operator =()?

Program prints:
In A(const char *str)

nothing more! Here is code:
class A {
public:
char *s;
public:
A(): s(NULL) { cout << "In A()" << endl;}
A(const char *str) { cout << "In A(const char *str)" << endl; s = new
char[strlen(str)]; strcpy(s, str); }
A &operator =(const A &rhs);

//~A() { if (s) delete[] s; }
};
A &A::eek:perator =(const A &rhs) {
cout << "in A &operator =(const A &rhs)" << endl;
int size = strlen(rhs.s);
s = new char[size];
strcpy(s, rhs.s);
return *this; }

Others have already commented on why operator= is not called in the code
below. I just want to point out that your assignment operator leaks memory
and does not do what you want when you do a self-assignment

a = a;

You should consider not using char* but std::string. Very likely, it would
be more efficient too, since std::string keeps track of its length and
therefore does not need a linear time search like strlen() to know how much
space to allocate.
int main() {
A a1("ABCDE");
A a2 = a1;
return 0;
}



Best

Kai-Uwe Bux
 
R

red floyd

Pierre said:
Because it is another syntax for:

A a2(a1);

Except for an explicit constructor. The Standard states that if a class
has an explicit constructor, the former syntax (A a2 = a1) won't work.
Don't have my Standard here -- I'm on the road -- so I can't quote
chapter and verse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top