Reputation: 1371
Currently learning Ada and actually enjoying it, there is a something that bothers me: what's a tagged
type? According to Programming in Ada 2012 by John Barnes, it indicates that the instantiated object carries a tag at run time.
I never heard of anything like this in C++ or C I think, so I'm a bit lost. What is it? When do I need it (apparently for having methods and inheritance?)?
Upvotes: 6
Views: 1077
Reputation: 3898
Tag in Ada is much like a Virtual Function Table Pointer in C++. That is, tagged type is the type having this one.
Virtual Function Table Pointer is allocated in the structure/class as soon as it have declared it's first virtual
function. In Ada, you just have to declare it explicitly.
Both Ada's Tag and C++'s VFTPtr make a dynamic dispatch available.
Upvotes: 3
Reputation: 1713
It is simply a class. That's a way in Ada to declare the root of a class hierarchy. Another way is to use interfaces.
It is also, at the moment, the way to obtain dot notation for a type (but this will be generalized in Ada 2022).
So you rarely if ever directly manipulate the tag, just the same as vtables are behind the scenes providing the dispatching but you don't need to think about them in C++.
A notable difference with these languages is that T'Class
can be used to refer to a whole family of derived types, and it must be used explicitly to achieve dynamic dispatch.
Upvotes: 6