Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The definition of (non-virtual) "overriding" in C++

50 views
Skip to first unread message

JiiPee

unread,
Feb 1, 2015, 1:34:38 PM2/1/15
to
I know that for virtual call we say that the derived class function is
overriding the base class version, if they have the same function. But
how about in a case where its a non-virtual situation:

class A
{
public:
void foo() {}
};

class B : public A
{
public:
void foo() {}
};

Then we use B object:
B b;
b.foo();

In this case are we saying (/can we say) that B::foo is overriding
A::foo? I think we can say A::foo is "hiding" or we are "redefining foo"
but is this also overriding? I am talking about definitions. I have
found many websites where they call this "overriding" (like tutorial web
sites), see:

http://www.programiz.com/cpp-programming/function-overriding
http://www.techopedia.com/definition/24010/overriding
http://www.sciencehq.com/computing-technology/overriding-member-function-in-c.html
http://en.wikipedia.org/wiki/Method_overriding
http://xytang.blogspot.co.uk/2007/05/comparisons-of-c-and-java-iii-method.html


Also on wikipedia it says: "*Method overriding*, in object oriented
programming <http://en.wikipedia.org/wiki/Object_oriented_programming>,
is a language feature that allows a subclass
<http://en.wikipedia.org/wiki/Subclass_%28computer_science%29> or child
class to provide a specific implementation of a method
<http://en.wikipedia.org/wiki/Method_%28computer_science%29> that is
already provided by one of its superclasses
<http://en.wikipedia.org/wiki/Superclass_%28computer_science%29> or
parent classes. ... The version of a method that is executed will be
determined by the object
<http://en.wikipedia.org/wiki/Object_%28computer_science%29> that is
used to invoke it. "
So it does not speak anything about polymorphism there... meaning
that my example is "overriding" according to wikipedias definition for
that word. Or am I missing something?

Where can I find the official definition for the word "overriding" in
programming? I tried to google but did not find really, only this wikipedia.

Paavo Helde

unread,
Feb 1, 2015, 2:33:28 PM2/1/15
to
JiiPee <n...@notvalid.com> wrote in news:Vkuzw.468124$lm5.4...@fx36.am4:

> I know that for virtual call we say that the derived class function is
> overriding the base class version, if they have the same function. But
> how about in a case where its a non-virtual situation:
[...]
> Where can I find the official definition for the word "overriding" in
> programming? I tried to google but did not find really, only this
> wikipedia.

I doubt there is any "official definition" of any word in "programming".
But for C++ it is easier, we have an official C++ standard. It appears it
is using term "hiding" for non-virtual functions and reserves "overriding"
for virtual functions (see esp 10.2):

"A member name f in one subobject B hides a member name f in a sub-object A
if A is a base class sub-object of B."

The word "hides" is typed in italic, meaning this sentence defines the
meaning of "hiding".

Also note that virtual function overriding requires exact match of the
virtual function signatures, but hiding has broader sweep and hides all
base class members with the same name, regardless of signatures (though one
can unhide other signatures by a using-declaration).

hth
Paavo

JiiPee

unread,
Feb 1, 2015, 3:09:03 PM2/1/15
to
On 01/02/2015 19:33, Paavo Helde wrote:
> JiiPee <n...@notvalid.com> wrote in news:Vkuzw.468124$lm5.4...@fx36.am4:
>
>> I know that for virtual call we say that the derived class function is
>> overriding the base class version, if they have the same function. But
>> how about in a case where its a non-virtual situation:
> [...]
>> Where can I find the official definition for the word "overriding" in
>> programming? I tried to google but did not find really, only this
>> wikipedia.
> I doubt there is any "official definition" of any word in "programming".
> But for C++ it is easier, we have an official C++ standard. It appears it
> is using term "hiding" for non-virtual functions and reserves "overriding"

> for virtual functions (see esp 10.2):
>
> "A member name f in one subobject B hides a member name f in a sub-object A
> if A is a base class sub-object of B."

But this does not mean that it cannot *also* be overriding. It could be
both. This is the definition of hiding, but where is the definition of
"overriding"?

Öö Tiib

unread,
Feb 1, 2015, 3:49:54 PM2/1/15
to
When you *override* only some of overloads in base class then you
*hide* rest of the overloads. That is confusing to most people and
so most coding standards suggest against it.
Do not hide public virtual functions of base class. Either override
(or unhide with using declaration) all overloads of base class or none.

> This is the definition of hiding, but where is the definition of
> "overriding"?

Method overriding is defined by object oriented programming
(not only for C++). As for C++ 'override' is even a a keyword in it.
Coding standards suggest to use that keyword for all overrides
to reduce defects by little deviations in virtual function signatures.

JiiPee

unread,
Feb 1, 2015, 3:53:18 PM2/1/15
to
ok, but you are talking about virtual functions here. How about for
non-virtual functions? can you "override" a non-virtual function.. that
was my question.?

JiiPee

unread,
Feb 1, 2015, 3:56:46 PM2/1/15
to
yes I know, the override-keyword in C++ refers to virtual functions. But
if we are talking about general use of override -word (not the c++ keyword).

Öö Tiib

unread,
Feb 1, 2015, 4:00:38 PM2/1/15
to
No. You can not override (IOW replace behavior of) non virtual member
function of base class.

JiiPee

unread,
Feb 1, 2015, 4:10:22 PM2/1/15
to
On 01/02/2015 21:00, Öö Tiib wrote:
>
>
> No. You can not override (IOW replace behavior of) non virtual member
> function of base class.

So we are "redefining" the base class version?


Chris Vine

unread,
Feb 1, 2015, 5:19:47 PM2/1/15
to
The word "overrides" is in italics as a defined expression in §10.3/2 of
C++11 (as also is "final overrider"), if that is what is bothering you,
and is applied exclusively to virtual functions. §10.3 contains a
lengthy exposition of overriding. In C++11, "override" (no 's') is a
keyword applicable only to virtual functions.

Words are just labels. In C++ usage, "overriding" is applied to
virtual functions where polymorphic behaviour is expected, and "hiding"
is applied to non-virtual functions where you have static dispatch.

In discussing your code you can give the names any meaning you want,
provided YOU make it clear what you mean. But why not stick to common
usage? And what has pushed your button so hard on this?

Chris

Chris Vine

unread,
Feb 1, 2015, 5:24:40 PM2/1/15
to
On Sun, 1 Feb 2015 22:19:37 +0000
Chris Vine <chris@cvine--nospam--.freeserve.co.uk> wrote:
> Words are just labels. In C++ usage, "overriding" is applied to
> virtual functions where polymorphic behaviour is expected, and
> "hiding" is applied to non-virtual functions where you have static
> dispatch.

Or, to be complete, "hiding" can also apply to virtual functions if you
have hidden one (usually by mistake because it is poor design) by
reusing a name with a different signature, thus dispensing with
polymorphic behaviour.

Chris

Paavo Helde

unread,
Feb 1, 2015, 5:29:36 PM2/1/15
to
JiiPee <n...@notvalid.com> wrote in news:pJvzw.698107$E64.4...@fx13.am4:
10.3/2:

"If a virtual member function vf is declared in a class Base and in a class
Derived, derived directly or indirectly from Base, a member function vf
with the same name and same parameter list as Base::vf is declared, then
Derived::vf is also virtual (whether or not it is so declared) and it
overrides Base::vf."

("overrides" in italic)



JiiPee

unread,
Feb 1, 2015, 5:38:15 PM2/1/15
to
On 01/02/2015 22:19, Chris Vine wrote:
> The word "overrides" is in italics as a defined expression in §10.3/2 of
> C++11 (as also is "final overrider"), if that is what is bothering you,
> and is applied exclusively to virtual functions. §10.3 contains a
> lengthy exposition of overriding. In C++11, "override" (no 's') is a
> keyword applicable only to virtual functions.
>
> Words are just labels. In C++ usage, "overriding" is applied to
> virtual functions where polymorphic behaviour is expected, and "hiding"
> is applied to non-virtual functions where you have static dispatch.
>
> In discussing your code you can give the names any meaning you want,
> provided YOU make it clear what you mean. But why not stick to common
> usage? And what has pushed your button so hard on this?
>
> Chris

ok, so overriding has a different meaning in C++ than in other
languages. I am not english, but as far as i understand the word
"override" really means that instead of using something you are using
another thing. And thats what is happening here: when I write that foo
in B then instead of using A's foo we are using B's foo, so it kind of
"overrides" A's foo, isnt it? am just talking about english language
grammar here, not really C++ language. So it would be natural for me to
say that even non-virtual function overrides.



Chris Vine

unread,
Feb 1, 2015, 5:48:18 PM2/1/15
to
On Sun, 01 Feb 2015 22:37:58 +0000
JiiPee <n...@notvalid.com> wrote:
> ok, so overriding has a different meaning in C++ than in other
> languages. I am not english, but as far as i understand the word
> "override" really means that instead of using something you are using
> another thing. And thats what is happening here: when I write that
> foo in B then instead of using A's foo we are using B's foo, so it
> kind of "overrides" A's foo, isnt it? am just talking about english
> language grammar here, not really C++ language. So it would be
> natural for me to say that even non-virtual function overrides.

In english, the word "override" could well include what is referred to
in C++ as name hiding. But that is not how the word is defined and
used in the holy standard. So if you are discussing the C++ language
rather than the english language, the holy standard is what applies.

Chris

JiiPee

unread,
Feb 1, 2015, 5:48:26 PM2/1/15
to
ok, but still... logically speaking this does not say that non-virtual
similar thing could not be called "override" :). This 10.3/2 talks only
about virtual implementations, it does not say anything about
non-virtual situations.

So logically speaking it still does not exclude the possibility that
non-virtual function could be called "overrides".

What am saying is that 10.3/2 there does not *define* "overriding"
*generally* in C++, it only defines its use with virtual functions
there. But how about for non-virtual functions, my question remains.

If 10.3/2 was to define *generally* "override" in C++ language, it
should start something like: "Overriding is a term used to describe
polymorphism where base class virtual method is replaced with derived
class implementations. .... ". But 10.3/2 is not written like that.

You get my point?

>
>
>

Chris Vine

unread,
Feb 1, 2015, 5:55:00 PM2/1/15
to
On Sun, 01 Feb 2015 22:48:09 +0000
JiiPee <n...@notvalid.com> wrote:
[snip]
> You get my point?

No. Read the whole of §10.3 and it is quite clear.

Chris

JiiPee

unread,
Feb 1, 2015, 5:56:56 PM2/1/15
to
On 01/02/2015 22:48, Chris Vine wrote:
> On Sun, 01 Feb 2015 22:37:58 +0000
> JiiPee <n...@notvalid.com> wrote:
>> ok, so overriding has a different meaning in C++ than in other
>> languages. I am not english, but as far as i understand the word
>> "override" really means that instead of using something you are using
>> another thing. And thats what is happening here: when I write that
>> foo in B then instead of using A's foo we are using B's foo, so it
>> kind of "overrides" A's foo, isnt it? am just talking about english
>> language grammar here, not really C++ language. So it would be
>> natural for me to say that even non-virtual function overrides.
> In english, the word "override" could well include what is referred to
> in C++ as name hiding.

But hiding is not exactly the same as override anyway. With hiding the
parameter list and return value does not even need to match. And, hiding
is in a way also happening when there is a virtual base class function
(polymorhism), because hiding if basically that a new function
definition "blocks"
some base class function to be called anymore.

> But that is not how the word is defined and
> used in the holy standard. So if you are discussing the C++ language
> rather than the english language, the holy standard is what applies.
But I still have not found that the standard says like that. Somebody
showed me 2 places but none of them defines what "override" means
generally in C++. Am not saying you are not right, but I have not seen
the definition still yet. What "override" is defined in polymorphism has
been shown to me, but not what it means in non-polymorphism situation/case.

JiiPee

unread,
Feb 1, 2015, 5:59:48 PM2/1/15
to
can you post it here please. I dont know where to find it.

Again, I am talking about logic here. Yes, its possible the standard has
not been written totally logically here... true, and that is another
issue. But anyway, thats what I am discussing here.

In a court of law so far there is no evidence that "override" belongs
only to polynorphism :).

Chris Vine

unread,
Feb 1, 2015, 6:06:58 PM2/1/15
to
It has been explained to you concurrently by two people (me and
another) that in §10.3/2 of C++11 the word "overrides" is a defined
expression. Not an employed expression, but a _defined_ expression.
And it is defined and applied exclusively and only in relation to
virtual functions.

That's the the end of it, from the point of view of C++.

Get a good night's sleep as I think everything will become clear in the
morning. You are chasing shadows.

Chris

Paavo Helde

unread,
Feb 1, 2015, 6:10:56 PM2/1/15
to
JiiPee <n...@notvalid.com> wrote in news:hVxzw.540753$6k.7...@fx09.am4:

> On 01/02/2015 22:19, Chris Vine wrote:
>> The word "overrides" is in italics as a defined expression in
>> A§10.3/2 of C++11 (as also is "final overrider"), if that is what is
>> bothering you, and is applied exclusively to virtual functions.
>> A§10.3 contains a lengthy exposition of overriding. In C++11,
>> "override" (no 's') is a keyword applicable only to virtual
>> functions.
>>
>> Words are just labels. In C++ usage, "overriding" is applied to
>> virtual functions where polymorphic behaviour is expected, and
>> "hiding" is applied to non-virtual functions where you have static
>> dispatch.
>>
>> In discussing your code you can give the names any meaning you want,
>> provided YOU make it clear what you mean. But why not stick to common
>> usage? And what has pushed your button so hard on this?
>>
>> Chris
>
> ok, so overriding has a different meaning in C++ than in other
> languages. I am not english, but as far as i understand the word
> "override" really means that instead of using something you are using
> another thing. And thats what is happening here: when I write that foo
> in B then instead of using A's foo we are using B's foo, so it kind of
> "overrides" A's foo, isnt it? am just talking about english language
> grammar here, not really C++ language. So it would be natural for me
> to say that even non-virtual function overrides.

In order to see the "override" effect with the non-virtual functions you
need also to use an object of another type (or a pointer or reference to
another type). It is natural that an object of another type can have
different member functions than the first type, even if they happen to
share the same name and parameter list. In this sense, you are not
overriding anything, you are just using another type.

With virtual functions, the aim is to use pointers or references to the
base type and get the functionality of the actual (most derived) type.
This is quote another thing (and what OOP is mostly about).

That said, I believe most C++ folks will understand what you mean when
you are talking about overriding of non-virtual functions, just keep in
mind this is not quite correct technically.

Cheers
Paavo

Paavo Helde

unread,
Feb 1, 2015, 6:22:11 PM2/1/15
to
JiiPee <n...@notvalid.com> wrote in news:Q2yzw.527342$w_7.2...@fx38.am4:

> So logically speaking it still does not exclude the possibility that
> non-virtual function could be called "overrides".
>
> What am saying is that 10.3/2 there does not *define* "overriding"
> *generally* in C++, it only defines its use with virtual functions
> there. But how about for non-virtual functions, my question remains.

The C++ standard only defines what is there. It does not define what is not
there (that would be infinite amont of things!). And I searched through the
standard and did not find any other definitions for "override". Is that
sufficient to calm you down?

For C++ standard, see http://lmgtfy.com/?q=latest+c%2B%2B+standard+download

Cheers
Paavo

JiiPee

unread,
Feb 1, 2015, 6:33:16 PM2/1/15
to
On 01/02/2015 23:22, Paavo Helde wrote:
> JiiPee <n...@notvalid.com> wrote in news:Q2yzw.527342$w_7.2...@fx38.am4:
>
>> So logically speaking it still does not exclude the possibility that
>> non-virtual function could be called "overrides".
>>
>> What am saying is that 10.3/2 there does not *define* "overriding"
>> *generally* in C++, it only defines its use with virtual functions
>> there. But how about for non-virtual functions, my question remains.
> The C++ standard only defines what is there. It does not define what is not
> there (that would be infinite amont of things!). And I searched through the
> standard and did not find any other definitions for "override". Is that
> sufficient to calm you down?

there is nothing to calm down, we are just discussing about this matter :)
ok, thanks. I ll download it

Öö Tiib

unread,
Feb 1, 2015, 6:40:05 PM2/1/15
to
On Monday, 2 February 2015 00:59:48 UTC+2, JiiPee wrote:
> On 01/02/2015 22:54, Chris Vine wrote:
> > On Sun, 01 Feb 2015 22:48:09 +0000
> > JiiPee <n...@notvalid.com> wrote:
> > [snip]
> >> You get my point?
> > No. Read the whole of §10.3 and it is quite clear.
> >
> > Chris
> >
>
> can you post it here please. I dont know where to find it.
>
> Again, I am talking about logic here. Yes, its possible the standard has
> not been written totally logically here... true, and that is another
> issue. But anyway, thats what I am discussing here.

Nothing is wrong with your logic so far. In some programming
languages (Javascript, Objective-C) you can override all methods
of base class and in others (C++, C#, Delphi) you can override only
functions that are marked as virtual.

> In a court of law so far there is no evidence that "override" belongs
> only to polynorphism :).

That logic I do not understand.
Modifying and extending behavior of a class *is* polymorphism.

JiiPee

unread,
Feb 1, 2015, 6:44:04 PM2/1/15
to
On 01/02/2015 23:22, Paavo Helde wrote:
how about at page 1053 where it says "309) Classes derived from
basic_streambuf can provide more efficient ways to implement xsgetn()
and xsputn() by **overriding**
these definitions from the base class."

My ** .... So seems like there is an example where they are talking
about overriding a base class non-virtual function in a derived class.
The header of xsputn () is (i think):

streamsize xsputn (const char_type* s, streamsize n);

so its not a virtual function.


JiiPee

unread,
Feb 1, 2015, 6:49:59 PM2/1/15
to
heh, sorry I ll take this back... it is actually a virtual function :),
checked from the code (cplusplus site did not show this). Ok, I will
sleep night over and will think about this :) ...

Öö Tiib

unread,
Feb 1, 2015, 6:51:01 PM2/1/15
to
You said you did download standard?
http://www.cplusplus.com/reference/streambuf/streambuf/xsgetn/
"protected virtual member function"

JiiPee

unread,
Feb 1, 2015, 6:52:37 PM2/1/15
to
On 01/02/2015 23:22, Paavo Helde wrote:
yes, I checked also.... ok then, I accept this as its never used on
non-virtual functions there.

JiiPee

unread,
Feb 1, 2015, 6:53:43 PM2/1/15
to
yes I just corrected it.... the site I referred did not mention that

JiiPee

unread,
Feb 1, 2015, 6:58:47 PM2/1/15
to
On 01/02/2015 23:39, Öö Tiib wrote:
> On Monday, 2 February 2015 00:59:48 UTC+2, JiiPee wrote:
>> On 01/02/2015 22:54, Chris Vine wrote:
>>> On Sun, 01 Feb 2015 22:48:09 +0000
>>> JiiPee <n...@notvalid.com> wrote:
>>> [snip]
>>>> You get my point?
>>> No. Read the whole of §10.3 and it is quite clear.
>>>
>>> Chris
>>>
>> can you post it here please. I dont know where to find it.
>>
>> Again, I am talking about logic here. Yes, its possible the standard has
>> not been written totally logically here... true, and that is another
>> issue. But anyway, thats what I am discussing here.
> Nothing is wrong with your logic so far. In some programming
> languages (Javascript, Objective-C) you can override all methods
> of base class and in others (C++, C#, Delphi) you can override only
> functions that are marked as virtual.

ok, let it be like that then.

Some of those websites then are confusing by using override with
non-virtual functions. Maybe they are former Java programmers :)

>
>> In a court of law so far there is no evidence that "override" belongs
>> only to polynorphism :).
> That logic I do not understand.
> Modifying and extending behavior of a class *is* polymorphism.

ok but now I have more evidence , because I checked the whole standard.

>

JiiPee

unread,
Feb 1, 2015, 7:06:27 PM2/1/15
to
On 01/02/2015 23:22, Paavo Helde wrote:
heh, that standand pdf seems to be pretty readable and
understandable.... maybe best to read it to learn C++ :). That way one
can be sure is learning the right things.
0 new messages