You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Abstract classes act as expressions of general concepts from which more specific classes can be derived. You can't create an object of an abstract class type. However, you can use pointers and references to abstract class types.
10
10
11
-
You create an abstract class by declaring at least one pure virtual member function. That's a virtual function declared by using the pure specifier (`= 0`) syntax. Classes derived from the abstract class must implement the pure virtual function or they, too, are abstract classes.
11
+
You create an abstract class by declaring at least one pure virtual member function. That's a virtual function declared by using the *pure* specifier (`= 0`) syntax. Classes derived from the abstract class must implement the pure virtual function or they, too, are abstract classes.
12
12
13
13
Consider the example presented in [Virtual functions](../cpp/virtual-functions.md). The intent of class `Account` is to provide general functionality, but objects of type `Account` are too general to be useful. That means `Account` is a good candidate for an abstract class:
14
14
@@ -57,9 +57,14 @@ class base
57
57
{
58
58
public:
59
59
base() {}
60
-
virtual ~base() = 0 {}; // pure virtual, and defined!
60
+
// To define the virtual destructor outside the class:
61
+
virtual ~base() = 0;
62
+
// Microsoft-specific extension to define it inline:
63
+
// virtual ~base() = 0 {};
61
64
};
62
65
66
+
base::~base() {} // required if not using Microsoft extension
67
+
63
68
class derived : public base
64
69
{
65
70
public:
@@ -73,7 +78,7 @@ int main()
73
78
}
74
79
```
75
80
76
-
The example shows the definition of `~base()` inline, but you can also define it outside the class by using `base::~base() {}`.
81
+
The example shows how a Microsoft compiler extension lets you add an inline definition to pure virtual `~base()`. You can also define it outside the class by using `base::~base() {}`.
77
82
78
83
When the object `aDerived` goes out of scope, the destructor for class `derived` is called. The compiler generates code to implicitly call the destructor for class `base` after the `derived` destructor. The empty implementation for the pure virtual function `~base` ensures that at least some implementation exists for the function. Without it, the linker generates an unresolved external symbol error for the implicit call.
0 commit comments