- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpure_virtual_function.cpp
More file actions
Latest commit
38 lines (30 loc) · 649 Bytes
/
Copy pathpure_virtual_function.cpp
File metadata and controls
38 lines (30 loc) · 649 Bytes
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
/* Compile options needed: none
*/
classA;
voidfcn( A* );
classA
{
public:
virtualvoidf() = 0;
A() { fcn( this ); }
};
classB : A
{
voidf() { }
};
voidfcn( A* p )
{
p->f();
}
// The declaration below invokes class B's constructor, which
// first calls class A's constructor, which calls fcn. Then
// fcn calls A::f, which is a pure virtual function, and
// this causes the run-time error. B has not been constructed
// at this point, so the B::f cannot be called. You would not
// want it to be called because it could depend on something
// in B that has not been initialized yet.
B b;
intmain(void)
{
return0;
}