Topic: Hide inherited virtual methods (on demand) instead of


Author: "volodymyr.ryvak via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Mon, 7 May 2018 07:59:06 -0700 (PDT)
Raw View
------=_Part_22723_1887148765.1525705146117
Content-Type: multipart/alternative;
 boundary="----=_Part_22724_556542870.1525705146117"

------=_Part_22724_556542870.1525705146117
Content-Type: text/plain; charset="UTF-8"

Hi.

If method is not virtual, you can simply "hide" it in derived class:
struct A
{
    void F() {}
};
struct B: public A
{
    int F() { return 0; }
};
But if method is virtual, compiler will try to override it (instead of
hide). In case of different result types this will leads to errors:
struct A
{
    virtual void F() {}
};
struct B: public A
{
    int F() { return 0; } // error, different result types
};
In C# such things easily makes via operator "new":
class A
{
    public virtual void F() { }
};
class B: A
{
    public new int F() { return 0; }
}
I propose to use the similar approach. For example operator "overload":
struct A
{
    virtual void F() {}
};
struct B: public A
{
    int F() overload { return 0; } // error, different result types
};
This will let us, for example, to use covariance (in general, as approach)
and smart pointers together:
// interface
struct IProduct {};
struct IPipeline
{
    virtual std::shared_ptr<IProduct> GetProduct();
};

// implementation
struct Product: IProduct {};
struct Pipeline: IPipeline
{
    std::shared_ptr<Product> GetProduct() overload;
};

// usage
Pipeline pipeline;
std::shared_ptr<Product> produce = pipeline.GetProduct();

Such approach may be used only in case of "overload" operator usage to keep
backward compatibility.

--
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/42a958cd-431a-48b5-b725-9793e210deff%40isocpp.org.

------=_Part_22724_556542870.1525705146117
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi.<br><br>If method is not virtual, you can simply &quot;=
hide&quot; it in derived class:<br>struct A<br>{<br>=C2=A0=C2=A0=C2=A0 void=
 F() {}<br>};<br>struct B: public A<br>{<br>=C2=A0=C2=A0=C2=A0 int F() { re=
turn 0; }<br>};<br>But if method is virtual, compiler will try to override =
it (instead of hide). In case of different result types this will leads to =
errors:<br>struct A<br>{<br>=C2=A0=C2=A0=C2=A0 virtual void F() {}<br>};<br=
>struct B: public A<br>{<br>=C2=A0=C2=A0=C2=A0 int F() { return 0; } // err=
or, different result types<br>};<br>In C# such things easily makes via oper=
ator &quot;new&quot;:<br>class A<br>{<br>=C2=A0=C2=A0=C2=A0 public virtual =
void F() { }<br>};<br>class B: A<br>{<br>=C2=A0=C2=A0=C2=A0 public new int =
F() { return 0; }<br>}<br>I propose to use the similar approach. For exampl=
e operator &quot;overload&quot;:<br>struct A<br>{<br>=C2=A0=C2=A0=C2=A0 vir=
tual void F() {}<br>};<br>struct B: public A<br>{<br>=C2=A0=C2=A0=C2=A0 int=
 F() overload { return 0; } // error, different result types<br>};<br>This =
will let us, for example, to use covariance (in general, as approach) and s=
mart pointers together:<br>// interface<br>struct IProduct {};<br>struct IP=
ipeline<br>{<br>=C2=A0=C2=A0=C2=A0 virtual std::shared_ptr&lt;IProduct&gt; =
GetProduct();<br>};<br><br>// implementation<br>struct Product: IProduct {}=
;<br>struct Pipeline: IPipeline<br>{<br>=C2=A0=C2=A0=C2=A0 std::shared_ptr&=
lt;Product&gt; GetProduct() overload;<br>};<br><br>// usage<br>Pipeline pip=
eline;<br>std::shared_ptr&lt;Product&gt; produce =3D pipeline.GetProduct();=
<br><br>Such approach may be used only in case of &quot;overload&quot; oper=
ator usage to keep backward compatibility.<br></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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/42a958cd-431a-48b5-b725-9793e210deff%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/42a958cd-431a-48b5-b725-9793e210deff=
%40isocpp.org</a>.<br />

------=_Part_22724_556542870.1525705146117--

------=_Part_22723_1887148765.1525705146117--

.