A a2 = a1; no call to operator =?

Discussion in 'C++' started by moleskyca1@yahoo.com, Aug 17, 2006.

  1. Guest

    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;
    }
     
    , Aug 17, 2006
    #1
    1. Advertising

  2. 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::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;
    > }
    >
     
    Pierre Barbier de Reuille, Aug 17, 2006
    #2
    1. Advertising

  3. Noah Roberts Guest

    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::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;
    > }
     
    Noah Roberts, Aug 17, 2006
    #3
  4. Guest

    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::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
     
    , Aug 17, 2006
    #4
  5. Kai-Uwe Bux Guest

    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::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
     
    Kai-Uwe Bux, Aug 17, 2006
    #5
  6. red floyd Guest

    Pierre Barbier de Reuille wrote:
    > 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.
     
    red floyd, Aug 18, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    586
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    423
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,051
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    5,029
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,689
    Kanenas
    May 30, 2005
Loading...

Share This Page