Re: ctor, operator =

From: marbac (marbac_at_chello.at)
Date: 03/19/05


Date: Sat, 19 Mar 2005 11:10:27 GMT

Andrey Tarasevich schrieb:

> In general case - no. Assignment operator is normally applied to the
> already constructed object and in general case the implementation of the
> assignment operator will rely on that fact. In the code above you are
> trying to apply the assignment operator to an object, which has not been
> constructed yet. Whether this is a problem in your particular case
> depends on your class implementation details.

Well i reduced my code to a simple example to show my problem.
And I also found the mistake i made ... what a mess, because
it has nothing to do with the question "
copy constructor with operator = is valid?"

thanks a lot and regards marbac

#include <iostream>

class test {

public:
        test (int _size): size(_size) {
                if (size>0) array=new int [this->size];
                        else array=0;
                
        }
        
        test (test& src) {
                array=0; // THIS WAS MISSING!!
                *this=src;
        }
        
        ~test () {
                if (array) delete [] array;
        }
        
        test& operator = (const test& src) {
                size=src.size;
                if (array) delete [] array;
                if (size>0) array=new int [this->size];
                        else array=0;
                return *this;
        }
        
        void cout_size () {
                std::cout << "Size:" << size << std::endl;
        }
                
private:
        int *array;
        int size;
};

int main () {
        test a(4);
        test b(5);
        test c(6);
        
        a.cout_size();
        b.cout_size();
        c.cout_size();
        
        a=b;
        a.cout_size();
        
        test d (a); //runtime - error if deleting non 0 pointer at construction
time
        d.cout_size ();
}



Relevant Pages

  • Re: ctor, operator =
    ... > In this case, is following ctor valid? ... In general case - no. Assignment operator is normally applied to the ... already constructed object and in general case the implementation of the ... depends on your class implementation details. ...
    (comp.lang.cpp)
  • Re: Questions of copy constructor
    ... >> have a copy constructor and assignment operator. ... assignment operator is that a class manages or "owns" some resource. ... Such classes often contain pointers, but the presence of a pointer ... >> itself has a pointer member or not? ...
    (comp.lang.cpp)
  • Re: Does std::map provides a copy assignment operator?
    ... assignment and copy constructor. ... > If the compiler-manufactured assignment operator (and also copy ... > destructors if the compiler-generated version does the wrong thing. ... > because the compiler versions will do the right thing for and call the ...
    (microsoft.public.vc.stl)
  • Re: implementation details
    ... > to investigating the importance of templates. ... > Retrieve then I'd like to implement some sort of overrun check. ... > an implementation of the assignment operator that'll compare two ... > buffers and a copy constructor that'll do just that. ...
    (comp.lang.cpp)
  • Re: bitset assignment & copy constructor
    ... This is part of C++ language specification, not some kind of implementation-specific behavior. ... So whenever I provid a copy constructor or assignment operator in my derived STL class the compiler will take mines, in case I don't the compiler takes the default ones for me correct? ...
    (microsoft.public.vc.stl)