which function is called

which function is called

when a derived object calls the base class member function, inside which calls another virtual member function, which is implemented inside the derived class, so which virtual member function is actually called ?

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
#include <iostream>
class Weld {
public:
Weld(){};
virtual ~Weld(){};
virtual bool getDataIndex(){
std::cout << "Weld::getDataIndex" << std::endl;
return true ;
}
bool getInternalDataList(){
return getDataIndex();
}
};
class SpotWeld: public Weld{
public:
SpotWeld(){};
virtual ~SpotWeld(){};
virtual bool getDataIndex(){
std::cout << "SpotWeld::getDataIndex" << std::endl;
return true;
}
};
class ACM2 : public SpotWeld{
public:
ACM2(){};
~ACM2(){};
virtual bool getDataIndex(){
std::cout << "ACM2::getDataIndex" << std::endl;
return true;
}
bool getPoints(){
return getInternalDataList();
}
};
int main(){
ACM2 acm2 ;
acm2.getPoints();
return 0;
}

output is:

ACM2::getDataIndex

so the derived object always calls the member function that most closest to its own class domain first, even if this member function is called from a base class member function.