//Given the following program , what is the output for each of the numbered lines in main()
#include <iostream>
using namespace std;
class X{
public:
    X() { cerr << "X()|";}
    X (const X&) { cerr << "X(const X&)|";}
    ~X() { cerr << "~X()|";}
    X &operator=(const X&) { cerr << "X::op=|"; return *this;}
};
class B{
public:
    B() { cerr << "B()|";}
    B (const B&) { cerr << "B(const B&)|";}
    virtual ~B() { cerr << "~B()|";}

    B &operator=(const B&) { cerr << "B::op=|"; return *this;}
};
class D:public B{
public:
    D() { cerr << "D()|";}
    D (const D& d):B(d),x_(d.x_) { cerr << "D(const D&)|";}
    virtual ~D() { cerr << "~D()|";}
    D &operator=(const D& d) {
    cerr << "D::op=|"; 
    B::operator=(d);
    x_ = d.x_;

    return *this;
    }
private:
X x_;   
};
int main()
{
//B b[2];
//B b1(b[0]);
B *pb = new D;
//B b1(b[0]);

}

When I compile and run the program it prints B()|X()|D()|
I do not understand why it is calling X's constructor.

It appears to me that an X object is created as a member of D, which should call X's constructor when a D object is created.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.