Topic: Use of "implement" when overriding a virtual method
Author: cristian64@gmail.com
Date: Wed, 28 Feb 2018 14:04:42 -0800 (PST)
Raw View
------=_Part_6260_1113630154.1519855482475
Content-Type: multipart/alternative;
boundary="----=_Part_6261_1256850492.1519855482476"
------=_Part_6261_1256850492.1519855482476
Content-Type: text/plain; charset="UTF-8"
Pretty often I find myself wanting to declare a method as *implement*, as
opposed to using *override*.
Here an example:
#include <iostream>
class A
{
public:
virtual ~A() {}
virtual void foo() { std::cout << "A::foo" << std::endl; }
virtual void bar() = 0;
};
class B : public A
{
public:
void foo() override { std::cout << "B::foo" << std::endl; }
void bar() implement { std::cout << "B::bar" << std::endl; }
};
It helps readability. The use of *override* implies that an implementation
already exists and is going to be overridden, which is not the case for
*A::bar*.
*implement* would just fit in this case.
If in the future an implementation for *A::bar* is provided, the compiler
will let
the developer know that an implementation is now being overridden, that is
no
longer "implementing it", but "overriding it", which may be bad. I, as a
developer,
want to be aware of that scenario. I would be able to decide whether the
base
implementation needs to be executed inside the derived class'
implementation,
and *when* it must be executed.
For retro-compatibility, *override* could always be used where the use of
*implement* is allowed.
On the other hand, *implement* would become a reserved keyword of the C++
programing language.
Thoughts?
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/8462ee66-9e9a-4579-8246-64861bf4a349%40isocpp.org.
------=_Part_6261_1256850492.1519855482476
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Pretty often I find myself wanting to declare a method as =
<b>implement</b>, as<br>opposed to using <b>override</b>.<br><br>Here an ex=
ample:<br><br><div style=3D"background-color: rgb(250, 250, 250); border-co=
lor: rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-w=
rap: break-word;" class=3D"prettyprint"><font size=3D"2"><code class=3D"pre=
ttyprint"><div class=3D"subprettyprint"><span style=3D"color: rgb(136, 0, 0=
);" class=3D"styled-by-prettify">#include</span><span style=3D"color: rgb(0=
, 0, 0);" class=3D"styled-by-prettify"> </span><span style=3D"color: rgb(0,=
136, 0);" class=3D"styled-by-prettify"><iostream></span><span style=
=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"><br><br></span><span=
style=3D"color: rgb(0, 0, 136);" class=3D"styled-by-prettify">class</span>=
<span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> A<br></s=
pan><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-prettify">{=
</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"><b=
r></span><span style=3D"color: rgb(0, 0, 136);" class=3D"styled-by-prettify=
">public</span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-=
prettify">:</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-p=
rettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: rgb(0, 0, 136);" cl=
ass=3D"styled-by-prettify">virtual</span><span style=3D"color: rgb(0, 0, 0)=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: rgb(102, 102,=
0);" class=3D"styled-by-prettify">~</span><span style=3D"color: rgb(0, 0, =
0);" class=3D"styled-by-prettify">A</span><span style=3D"color: rgb(102, 10=
2, 0);" class=3D"styled-by-prettify">()</span><span style=3D"color: rgb(0, =
0, 0);" class=3D"styled-by-prettify"> </span><span style=3D"color: rgb(102,=
102, 0);" class=3D"styled-by-prettify">{}</span><span style=3D"color: rgb(=
0, 0, 0);" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span styl=
e=3D"color: rgb(0, 0, 136);" class=3D"styled-by-prettify">virtual</span><sp=
an style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: rgb(0, 0, 136);" class=3D"styled-by-prettify">void</span>=
<span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> foo</spa=
n><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-prettify">()<=
/span><code class=3D"prettyprint"><span style=3D"color: rgb(0, 0, 0);" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: rgb(102, 102, 0);" c=
lass=3D"styled-by-prettify">{</span><span style=3D"color: rgb(0, 0, 0);" cl=
ass=3D"styled-by-prettify"> std</span><span style=3D"color: rgb(102, 102, 0=
);" class=3D"styled-by-prettify">::</span><span style=3D"color: rgb(0, 0, 0=
);" class=3D"styled-by-prettify">cout </span><span style=3D"color: rgb(102,=
102, 0);" class=3D"styled-by-prettify"><<</span><span style=3D"color=
: rgb(0, 0, 0);" class=3D"styled-by-prettify"> </span><span style=3D"color:=
rgb(0, 136, 0);" class=3D"styled-by-prettify">"A::foo"</span><sp=
an style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-prettify"><<<=
/span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> st=
d</span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-prettif=
y">::</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettif=
y">endl</span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-p=
rettify">;</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by=
-prettify">}</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-=
prettify"></span></code><span style=3D"color: rgb(0, 0, 0);" class=3D"style=
d-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: rgb(0, 0, 136=
);" class=3D"styled-by-prettify">virtual</span><span style=3D"color: rgb(0,=
0, 0);" class=3D"styled-by-prettify"> </span><span style=3D"color: rgb(0, =
0, 136);" class=3D"styled-by-prettify">void</span><span style=3D"color: rgb=
(0, 0, 0);" class=3D"styled-by-prettify"> bar</span><span style=3D"color: r=
gb(102, 102, 0);" class=3D"styled-by-prettify">()</span><span style=3D"colo=
r: rgb(0, 0, 0);" class=3D"styled-by-prettify"> =3D 0;</span><span style=3D=
"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"><br></span><span style=
=3D"color: rgb(102, 102, 0);" class=3D"styled-by-prettify">};</span><span s=
tyle=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"><br>=C2=A0<br></=
span><span style=3D"color: rgb(0, 0, 136);" class=3D"styled-by-prettify">cl=
ass</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"=
> B </span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-pret=
tify">:</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: rgb(0, 0, 136);" class=3D"styled-by-pret=
tify">public</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-=
prettify"> A<br></span><span style=3D"color: rgb(102, 102, 0);" class=3D"st=
yled-by-prettify">{</span><span style=3D"color: rgb(0, 0, 0);" class=3D"sty=
led-by-prettify"><br></span><span style=3D"color: rgb(0, 0, 136);" class=3D=
"styled-by-prettify">public</span><span style=3D"color: rgb(102, 102, 0);" =
class=3D"styled-by-prettify">:</span><span style=3D"color: rgb(0, 0, 0);" c=
lass=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: =
rgb(0, 0, 136);" class=3D"styled-by-prettify">void</span><span style=3D"col=
or: rgb(0, 0, 0);" class=3D"styled-by-prettify"> foo</span><span style=3D"c=
olor: rgb(102, 102, 0);" class=3D"styled-by-prettify">()</span><span style=
=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> </span><span style=
=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"><code class=3D"prett=
yprint"><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"><=
/span><span style=3D"color: rgb(0, 0, 136);" class=3D"styled-by-prettify">o=
verride</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prett=
ify"></span></code> </span><span style=3D"color: rgb(102, 102, 0);" class=
=3D"styled-by-prettify">{</span><span style=3D"color: rgb(0, 0, 0);" class=
=3D"styled-by-prettify"> std</span><span style=3D"color: rgb(102, 102, 0);"=
class=3D"styled-by-prettify">::</span><span style=3D"color: rgb(0, 0, 0);"=
class=3D"styled-by-prettify">cout </span><span style=3D"color: rgb(102, 10=
2, 0);" class=3D"styled-by-prettify"><<</span><span style=3D"color: r=
gb(0, 0, 0);" class=3D"styled-by-prettify"> </span><span style=3D"color: rg=
b(0, 136, 0);" class=3D"styled-by-prettify">"B::foo"</span><span =
style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: rgb(102, 102, 0);" class=3D"styled-by-prettify"><<</sp=
an><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> std</=
span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-prettify">=
::</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify">=
endl</span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-pret=
tify">;</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-pr=
ettify">}</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-pre=
ttify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: rgb(0, 0, 136);" clas=
s=3D"styled-by-prettify">void</span><span style=3D"color: rgb(0, 0, 0);" cl=
ass=3D"styled-by-prettify"> bar</span><span style=3D"color: rgb(102, 102, 0=
);" class=3D"styled-by-prettify">()</span><span style=3D"color: rgb(0, 0, 0=
);" class=3D"styled-by-prettify"> implement</span><span style=3D"color: rgb=
(0, 0, 0);" class=3D"styled-by-prettify"> </span><span style=3D"color: rgb(=
102, 102, 0);" class=3D"styled-by-prettify">{</span><span style=3D"color: r=
gb(0, 0, 0);" class=3D"styled-by-prettify"> std</span><span style=3D"color:=
rgb(102, 102, 0);" class=3D"styled-by-prettify">::</span><span style=3D"co=
lor: rgb(0, 0, 0);" class=3D"styled-by-prettify">cout </span><span style=3D=
"color: rgb(102, 102, 0);" class=3D"styled-by-prettify"><<</span><spa=
n style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-prettify"> </span><span=
style=3D"color: rgb(0, 136, 0);" class=3D"styled-by-prettify">"B::bar=
"</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: rgb(102, 102, 0);" class=3D"styled-by-pre=
ttify"><<</span><span style=3D"color: rgb(0, 0, 0);" class=3D"styled-=
by-prettify"> std</span><span style=3D"color: rgb(102, 102, 0);" class=3D"s=
tyled-by-prettify">::</span><span style=3D"color: rgb(0, 0, 0);" class=3D"s=
tyled-by-prettify">endl</span><span style=3D"color: rgb(102, 102, 0);" clas=
s=3D"styled-by-prettify">;</span><span style=3D"color: rgb(0, 0, 0);" class=
=3D"styled-by-prettify"> </span><span style=3D"color: rgb(102, 102, 0);" cl=
ass=3D"styled-by-prettify">}</span><span style=3D"color: rgb(0, 0, 0);" cla=
ss=3D"styled-by-prettify"><br></span><span style=3D"color: rgb(102, 102, 0)=
;" class=3D"styled-by-prettify">};</span></div></code></font></div><br>It h=
elps readability. The use of <b>override</b> implies that an implementation=
<br>already exists and is going to be overridden, which is not the c<span s=
tyle=3D"font-family: arial, sans-serif;">ase for <b>A::bar</b>.</span><br><=
b>implement</b> would just fit in this case.<br><br>If in the future an imp=
lementation<span style=3D"font-family: arial, sans-serif;"> for <b>A::bar</=
b> is provide</span>d, the compiler will let<br>the developer know that an =
implementation is now being overridden, that is no<br>longer "implemen=
ting it", but "overriding it", which may be bad. I, as a dev=
eloper,<br>want to be aware of that scenario. I would be able to decide whe=
ther the base<br>implementation needs to be executed inside the derived cla=
ss' implementation,<br>and <i>when</i> it must be executed.<br><br>For =
retro-compatibility, <b>override</b> could always be used where the use of<=
br><b>implement</b> is allowed.<br><br>On the other hand, <b>implement</b> =
would become a reserved keyword of the C++<br>programing language.<br><br><=
br>Thoughts?<br></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/8462ee66-9e9a-4579-8246-64861bf4a349%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8462ee66-9e9a-4579-8246-64861bf4a349=
%40isocpp.org</a>.<br />
------=_Part_6261_1256850492.1519855482476--
------=_Part_6260_1113630154.1519855482475--
.