Re: Just curious...

Discussion in 'C++' started by Rui Maciel, Jan 5, 2013.

  1. Rui Maciel

    Rui Maciel Guest

    Paavo Helde wrote:

    > First, because logically Polygon has myfunction() (it has inherited it),
    > so why should it be banned to call it?
    >
    > Second, because otherwise the code would be forced to be both harder to
    > write and more fragile as well. First one would have to search in which
    > base class exactly the function has been defined and use e.g.
    > Shape::myfunction().


    Shape::myfunction() was never defined, which is precisely the problem.
    What's important to know is that C++ accepts classes that declare member
    functions which are never defined, and therefore the compiler won't throw an
    error when it compiles this sort of definition. Problems only occur once
    those member functions are actually called, which cause linker errors.


    > However, when someone later adds an actual
    > myfunction definition in Polygon, the code in Hexagon would probably
    > become invalid (this kind of function should most probably forward to the
    > nearest implementation which it replaces or overrides).


    I don't know what you mean by "become invalid". If class Polygon is
    declared with a Polygon::myfunction() then Hexagon::myfunction() would
    simply call it instead of Shape::myfunction(). The problem would stay the
    same: if Polygon::myfunction() is never defined then the code will compile
    just fine, but if there is a call to Polygon::myfunction(), directly or
    indirectly, then the linker will throw an error.


    Rui Maciel
     
    Rui Maciel, Jan 5, 2013
    #1
    1. Advertising

  2. Rui Maciel

    Rui Maciel Guest

    Paavo Helde wrote:

    > Sorry, we are not talking about Shape::myfunction(). OP was worried about
    > Polygon::myfunction().


    Polygon inherits Shape, and does not declare or define
    Polygon::myfunction(). Hence, when Polygon::myfunction() is called, what is
    actually being called is Shape::myfunction(). Which isn't defined. Hence,
    the linker error.


    Rui Maciel
     
    Rui Maciel, Jan 5, 2013
    #2
    1. Advertising

  3. Rui Maciel

    Rui Maciel Guest

    Rui Maciel wrote:

    > Polygon inherits Shape, and does not declare or define
    > Polygon::myfunction(). Hence, when Polygon::myfunction() is called, what
    > is actually being called is Shape::myfunction(). Which isn't defined.
    > Hence, the linker error.


    To be more specific, the standard states that «unless redeclared in the
    derived class, members of a base class are also considered to be members of
    the derived class». That's precisely what happens in Giuliano's example:
    Shape, the base class, declares a member function, myfunction(), and
    Polygon, the derived class, does not declare its own myfunction(). Hence,
    if you call Polygon::myfunction(), you are actually calling
    Shape::myfunction().


    Rui Maciel
     
    Rui Maciel, Jan 5, 2013
    #3
  4. Rui Maciel

    Rui Maciel Guest

    Paavo Helde wrote:

    > OP's example did not even include main(), so obviously it was not a
    > complete program and not meant for linking. It was just an
    > illustration/clarification accompanying the question (and served well in
    > this regard).


    The existence of a main() is irrelevant in this case. The question was if
    the compiler should throw an error if a member function is declared but not
    defined, the answer to which is clear: the compiler should not throw an
    error, because C++ clearly supports incomplete class definitions, but if the
    non-defined member function is called then the linker will obviously throw
    an error.


    RuiMaciel
     
    Rui Maciel, Jan 5, 2013
    #4
  5. Rui Maciel

    Rui Maciel Guest

    Paavo Helde wrote:

    > Sorry, no, the original question was:
    >
    > "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?"
    >
    > Polygon::myfunction() was never declared so your reasoning does not apply.


    You don't understand. In this example, Polygon::myfunction() is in fact
    Shape::myfunction(). Which wasn't defined. Hence, what I said.


    Rui Maciel
     
    Rui Maciel, Jan 6, 2013
    #5
  6. Rui Maciel <> wrote:
    > Paavo Helde wrote:
    >
    >> Sorry, no, the original question was:
    >>
    >> "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?"
    >>
    >> Polygon::myfunction() was never declared so your reasoning does not apply.

    >
    > You don't understand. In this example, Polygon::myfunction() is in fact
    > Shape::myfunction(). Which wasn't defined. Hence, what I said.


    That's true, but whether the function is actually defined or not is just
    not relevant to the question. I suspect, that in the real code, the method
    is actually defined and the OP just left it out because the implementation
    is not important to understand his question.

    Tobi.
     
    Tobias Müller, Jan 6, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    378
    Robert Klemme
    Jan 6, 2006
  2. Elaine Jackson

    just curious

    Elaine Jackson, Sep 6, 2003, in forum: Python
    Replies:
    3
    Views:
    372
    News M Claveau /Hamster-P
    Sep 6, 2003
  3. Ken Parkes

    Just curious

    Ken Parkes, Sep 20, 2004, in forum: Python
    Replies:
    4
    Views:
    327
    Ken Parkes
    Sep 22, 2004
  4. lallous
    Replies:
    5
    Views:
    355
  5. =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Gridview question - Just curious

    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Nov 12, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    307
    Eliyahu Goldin
    Nov 12, 2007
Loading...

Share This Page