view the default functions generated by a compiler?

后端 未结 5 1768
滥情空心
滥情空心 2020-12-28 22:17

Is there any way to view the default functions ( e.g., default copy constructor, default assignment operator ) generated by a compiler such as VC++2008 for a class which doe

5条回答
  •  伪装坚强ぢ
    2020-12-28 22:40

    Are you sure you need to view this functions?

    Per default the Compiler creates them by calling the Copy Constructor or Assignment operator on each member Variable.

    The Problem with that is that when you use Objects which use reference Counting for managing data the default Copy Constructor will create a Copy of the Object but not a copy of the Data the Object points to. This also is the case for pointers. So if your have a class like:

    class aClass
    {
      int one;
      int *ptwo;
    };
    

    The default Copy constructor only copy's the data a and the pointer b. However he does not copy the data pointed to by b. If you use this class like

    aClass a, b;
    a.ptwo = new int;
    a.one = 1;
    *(a.ptwo) = 2;
    
    b = a;
    
    *(b.ptwo) = 1;  
    //a.ptwo now points to an integer with the value of 1
    

    If you want this Class to copy the value of ptwo instead of the pointer you would need your own copy assignment operator function. If you are interested in more details what the default functions do and what not then you take a look into the Book "Effective C++".
    It has a whole chapter on this stuff, which explains what default functions of classes do, don't do, what they should do, when to write your own. I'm sure you can get a digital version on the web if you just want to know about this functionality.

提交回复
热议问题