view the default functions generated by a compiler?

后端 未结 5 1787
滥情空心
滥情空心 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:30

    The compiler generated methods are abstract they don't exist in source code.
    Look at the example below, I try and explain what the four compiler generated methods are supposed to do at the source code level. From this you should be able to extrapolate any normal class.

    If you have a class like this:

    class X: public Base
    {
        int*   a;
        double b;
        Y      c;
    };
    

    Then the compiler generates the equivalent of the following:

    X::X() // Default constructor
        :Base() Calls the base class default constructor
        //,a    pointers are POD no default initialization
        //,b    double   are POD no default initialization
        ,c()    //Call default constructor on each non POD member
    {}
    
    X::~X() // Default destructor
    {}
    // Destructor for each non POD member in reverse order
    ~c()       calls the destructor of the class type
    //~b       double are POD no destructor
    //~a       pointers are POD no destructor
    ~Base()    // Calls the base class destructor
    
    X::X(X const& copy)
        :Base(copy)    // calls the base class copy constructor
        // Copies each member using its copy constructor
        ,a(copy.a)     // Pointers copied  (Note just the pointer is copied, not what it points at)
        ,b(copy.b)     // Double copied.
        ,c(copy.c)     // Uses copy constructor of the class type (must be accessible)
    {}
    
    X& X::operator=(X const& copy)
    {
        Base::operator=(copy);  // Calls the base class assignment operator
        // Copies each member using the members assignment operator
        a = copy.a;    // Pointers copied  (Note just the pointer is copied, not what it points at)
        b = copy.b;    // Double copied
        c = copy.c;    // Uses assignment operator of the class type (must be accessible)
    
        return *this;
    }
    

提交回复
热议问题