Exercise to test self C++ comprehension

Hi!

Currently I'm doing exercises in the style "what does this program print?" in order to test if I understood correctly how things work in C++.

I have 2 doubts about the following piece of code (which is a shortened version of the real exercise):

*The following program compiles and runs*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;

class B {
public:
    B() {cout<< " B() ";}
    virtual void g() const {cout <<" B::g ";}
    virtual const B* j() {cout<<" B::j "; return this;}
    void m() {cout <<" B::m "; g(); j();}
    virtual B& n() {cout <<" B::n "; return *this;}
};

class C : virtual public B{
public:
    C() {cout<< " C() ";}
    virtual void g() const override {cout<<" C::g ";}
    virtual void m() {cout<<" C::m "; g(); j();}
    B& n() override {cout<<" C::n "; return *this; }
};

class D : virtual public B{
public:
    D() {cout<<" D() ";}
    virtual void g() {cout<<" D::g ";}
    void m() {cout<<" D::m "; g(); j();}
};

class E : public C, public D{
public:
    E() {cout<<" E() ";}
    virtual void g() const {cout<<" E::g ";}
    const E* j() {cout<<" E::j "; return this;}
    void m() {cout<<" E::m "; g(); j();}
    D& n() final {cout<<" E::n "; return *this;}
};

class F : public E{
public:
    F() {cout<<" F() ";}
    F(const F& x) : B(x) {cout<<" Fc ";}
    void m() {cout<<" F::m "; j();}
};


int main()
{
    F f;             //prints B() C() D() E() F()
    cout<<endl;
    C* p = new F(f); //prints C() D() E() Fc
    cout<<endl;
    D* q = new F(f); //prints C() D() E() Fc
    cout<<endl;
    C* p4 = new E(); //not interested in what this prints, we need this for the pointer
    cout<<endl;
    (p4->n()).m();   //prints E::n B::m E::g E::j
}


1) When this line get executed C* p = new F(f); I don't get why the B() subobject part of the F object doesn't get created.

I printed B() C() D() E() Fc, but the compiler prints is different.
Meanwhile the default construction of an F object prints also B().

What differs the copy construction and the default construction of an F object in this case?

2) When this line gets executed (p4->n()).m(); I don't understand what happens to the invocation object, in particular what happens to its dynamic type when the method n() returns the value.

In this statement I printed E::n D::m D::g E::j and I can't really tell what I am doing wrong.

Any help will be precious, thank you in advance!
Last edited on
1) You haven't overloaded B's copy ctor, and the default copy ctor doesn't print anything. Try adding this to B.

 
    B(const B& b) { cout << " B(b) "; }

Last edited on
Thank you for answering @dutch!

In the exercise I can't modify the code, I only have to say what do the statements in the main function print.

I understood now what you meant @dutch.
I totally missed that consctructor delegation to B copy constructor, thank you!
Last edited on
Topic archived. No new replies allowed.