One thing to clear about dynamic binding?

For example: If I have the base class

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
class cell_c {
public:

    virtual void calculateStep1();
    void calculateStep2();
    virtual void calculateStep3();
};

class uvCell_c:public cell_c {
public:

};

class uCell_c:public uvCell_c {
public:
    void calculateStep1();
    void calculateStep3();
};

class vCell_c:public uvCell_c {
public:
    void calculateStep1();
    void calculateStep3();
};

void cell_c::calculateStep2() {
    calculateStep1();
}

void uCell_c::calculateStep3() {
    calculateStep2();
}

void vCell_c::calculateStep3() {
    calculateStep2();
}


if I define

1
2
3
4
5
uCell_c U;
vCell_c V;

U.calculateStep3();//will this call uCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call uCell_c::calculateStep1?
V.calculateStep2();//will this call vCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call vCell_c::calculateStep1? 
U.calculateStep3();//will this call uCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call uCell_c::calculateStep1?
No.

uCell_c::calculateStep3 calls cell_c::calculateStep2.
cell_c::calculateStep2 calls calculateStep1() which is declared virtual, but a virtual override implementation doesn't exist in the code shown. This should cause a linker VTBL error.

V.calculateStep2();//will this call vCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call vCell_c::calculateStep1?
No.

V.calculateStep2() calls cell_c::calculateStep2().
Thanks, then how could I make it call the corresponding calculateStep1()?
how could I make it call the corresponding calculateStep1()?

Corresponding to what?

BTW, was V.calculateStep2() a typo? Did you mean V.calculateStep3() ?

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
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;

class cell_c {
public:
	cell_c();

    virtual void calculateStep1();
    void calculateStep2();
    virtual void calculateStep3();

    ~cell_c();//compiler error: Class 'cell_c' has virtual method 'calculateStep3' but non-virtual destructor
};

class uvCell_c:public cell_c {
public:

	uvCell_c();
    ~uvCell_c();//compiler error: Class 'uvCell_c' has virtual method 'calculateStep3' but non-virtual destructor
};

class uCell_c:public uvCell_c {
public:
    inline void calculateStep1() {cout<<"uCell"<<endl;}
    void calculateStep3();

    uCell_c();
    ~uCell_c();//compiler error: Class 'uCell_c' has virtual method 'calculateStep3' but non-virtual destructor
};

class vCell_c:public uvCell_c {
public:
    inline void calculateStep1() {cout<<"vCell"<<endl;}
    void calculateStep3();

    vCell_c();
    ~vCell_c();//compiler error: Class 'vCell_c' has virtual method 'calculateStep3' but non-virtual destructor
};

void cell_c::calculateStep2() {
    calculateStep1();
}

void uCell_c::calculateStep3() {
    calculateStep2();
    cout<<"step3 u"<<endl;
}

void vCell_c::calculateStep3() {
    calculateStep2();
    cout<<"step3 v"<<endl;
}


int main() {
	uCell_c U;//compiler error: undefined reference to `uCell_c::uCell_c()'
	vCell_c V;//compiler error: Multiple markers at this line
                       //	- undefined reference to 
                       //	 `vCell_c::~vCell_c()'
                       //	- undefined reference to 
                      //	 `vCell_c::vCell_c()'

	U.calculateStep3();//I want to see "uCell step3 u" print out
	V.calculateStep3();//I want to see "vCell step3 v" print out

	return 0;
}


The compiler error and what I want to do is written in comments, please help out! I think I don't know how to define a class with virtual member, and destructor stuff. Thanks.
Line 12, 19, 28, 37: Needs virtual keyword.
1
2
virtual ~cell_c();  //  Don't forget to write the implementations
{}


Line 56,57: That looks like a linker error, not a compiler error and implies you did not write the implementation for the uCell_c constructor.

Line 59,61: Appears you did not write the implementation for ~vCell_c()

Lines 63,64: Should print out once you get the compile and linker errors fixed.






Topic archived. No new replies allowed.