0
//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.

2
Contributors
1
Reply
13
Views
3 Years
Discussion Span
Last Post by tinstaafl
1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.