Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A a2 = a1; no call to operator =?

Reply
Thread Tools

A a2 = a1; no call to operator =?

 
 
moleskyca1@yahoo.com
Guest
Posts: n/a
 
      08-17-2006
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: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;
}

 
Reply With Quote
 
 
 
 
Pierre Barbier de Reuille
Guest
Posts: n/a
 
      08-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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: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;
> }
>

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      08-17-2006

(E-Mail Removed) wrote:
> 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: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;
> }


 
Reply With Quote
 
sonison.james@gmail.com
Guest
Posts: n/a
 
      08-17-2006
(E-Mail Removed) wrote:
> 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: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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-17-2006
(E-Mail Removed) wrote:

> 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: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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-18-2006
Pierre Barbier de Reuille wrote:
> (E-Mail Removed) wrote:
>> I cannot figure out why line A a2 = a1; won't call my operator =()?

>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
T::operator int () const ambiguous with T::operator Handle () const? Tim Clacy C++ 15 05-30-2005 02:14 AM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments