Default assignment operator= in c++ is a shallow copy?

前端 未结 8 2159
挽巷
挽巷 2020-12-14 05:42

Just a simple quick question which I couldn\'t find a solid answer to anywhere else. Is the default operator= just a shallow copy of all the class\' members on the right ha

8条回答
  •  萌比男神i
    2020-12-14 06:41

    As illustrated by the code snippet below, the = (assignment) operator for STL performs a deep copy.

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main(int argc, const char * argv[]) {
        /* performs deep copy */
        map  > m;
        stack  s1;
        stack  s2;
    
        s1.push(10);
        cout<<&s1<<" "<<&(s1.top())<<" "< > mp;
        stack  s1p;
        stack  s2p;
    
        s1p.push(new int);
        cout<<&s1p<<" "<<&(s1p.top())<<" "< v1,v2;
        vector v1p, v2p;
    
        v1.push_back(1);
        cout<<&v1<<" "<<&v1[0]<<" "<

提交回复
热议问题