Skip to main content
added 10 characters in body
Source Link
Erik Eidt
  • 34.4k
  • 6
  • 59
  • 94
double getAge(int id) override;

Is this also a virtual function?

Yes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

No, however, in another sense: the overriding method does not introduce a new virtual instance method (group) as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and the override is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of this term override is optional (and not supported in priorearlier C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).

double getAge(int id) override;

Is this also a virtual function?

Yes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

No, however, in another sense: the overriding method does not introduce a new virtual instance method as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and the override is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of override is optional (and not supported in prior C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).

double getAge(int id) override;

Is this also a virtual function?

Yes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

No, however, in another sense: the overriding method does not introduce a new virtual instance method (group) as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and the override is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of this term override is optional (and not supported earlier C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).

added 25 characters in body
Source Link
Erik Eidt
  • 34.4k
  • 6
  • 59
  • 94
double getAge(int id) override;

Is this also a virtual function?

YesYes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

NoNo, however, in another sense: the overriding method does not introduce a new virtual instance method as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and the override is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of override is optional (and not supported in prior C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).

double getAge(int id) override;

Is this also a virtual function?

Yes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

No, however, in another sense: the overriding method does not introduce a new virtual instance method as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of override is optional (and not supported in prior C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).

double getAge(int id) override;

Is this also a virtual function?

Yes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

No, however, in another sense: the overriding method does not introduce a new virtual instance method as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and the override is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of override is optional (and not supported in prior C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).

Source Link
Erik Eidt
  • 34.4k
  • 6
  • 59
  • 94

double getAge(int id) override;

Is this also a virtual function?

Yes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

No, however, in another sense: the overriding method does not introduce a new virtual instance method as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of override is optional (and not supported in prior C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).