Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: Just curious... (http://www.velocityreviews.com/forums/t956151-re-just-curious.html)

ctgqumgf@sharklasers.com 01-05-2013 09:57 AM

Re: Just curious...
 
Am Samstag, 5. Januar 2013 09:33:56 UTC+1 schrieb Giuliano Bertoletti:
> Hello, is it correct that the compiler does not issue an error on the following code? ===================== class Shape { public: void myfunction(); }; class Polygon : public Shape { public: // does not define myfunction }; class Hexagon : public Polygon{ public: void myfunction() { Polygon::myfunction(); // why does it compile? } }; ===================== I mean, what is the benefit of implicitly calling Shape::myfunction and not issuing an error when I'm explicitly asking to call a non existing Polygon::myfunction? Giulio.


Because that's exactly what public inheritance is for! A method defines
a certain functionality. Inheriting and NOT overriding a method means
the drived class has (wants to have) the same behaviour as the base class.
Some people call this phenomenon "reuse". A good starting point is
http://en.wikipedia.org/wiki/Inherit...ed_programming)

Victor Bazarov 01-05-2013 12:56 PM

Re: Just curious...
 
On 1/5/2013 4:57 AM, ctgqumgf@sharklasers.com wrote:
> Am Samstag, 5. Januar 2013 09:33:56 UTC+1 schrieb Giuliano Bertoletti:
>> Hello, is it correct that the compiler does not issue an error on
>> the

following code? ===================== class Shape { public: void
myfunction(); }; class Polygon : public Shape { public: // does not
define myfunction }; class Hexagon : public Polygon { public: void
myfunction() { Polygon::myfunction(); // why does it compile? } };
===================== I mean, what is the benefit of implicitly calling
Shape::myfunction and not issuing an error when I'm explicitly asking to
call a non existing Polygon::myfunction? Giulio.
>
> Because that's exactly what public inheritance is for! A method defines
> a certain functionality. Inheriting and NOT overriding a method means


Just a nitpick: you should only use the term 'overriding' when talking
about virtual functions. In the OP's example the function is not
virtual so no "overriding" can occur. You can *redefine* the function
in the derived class, but it will not *override* unless the function is
virtual and has the same type.

> the drived class has (wants to have) the same behaviour as the base class.
> Some people call this phenomenon "reuse". A good starting point is
> http://en.wikipedia.org/wiki/Inherit...ed_programming)
>


V
--
I do not respond to top-posted replies, please don't ask


All times are GMT. The time now is 04:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.