final methods/functions

Discussion in 'C++' started by Shea Martin, Jul 25, 2005.

  1. Shea Martin

    Shea Martin Guest

    I have a pure virtual f'n bool SaveData(). This f'n is called by
    SaveDataWithWarn(). The idea being the implementing class must define
    how data is saved, but the base class handles GUI operations, and user
    interaction.

    SaveDataWithWarn is called by a GUI event trigger. But it is possible
    that an implementing class would like to manually call this function.
    Thus it must be atleast public. But I don't want the base class to over
    ride it. I believ in Java I can put 'final' as a modifier. Is there an
    equiv in C++, besides a comment like this:
    //over-riding this function will result in a pink-slip.

    Thanks,

    ~S
     
    Shea Martin, Jul 25, 2005
    #1
    1. Advertising

  2. Shea Martin sade:
    > I have a pure virtual f'n bool SaveData(). This f'n is called by
    > SaveDataWithWarn(). The idea being the implementing class must define
    > how data is saved, but the base class handles GUI operations, and user
    > interaction.
    >
    > SaveDataWithWarn is called by a GUI event trigger. But it is possible
    > that an implementing class would like to manually call this function.
    > Thus it must be atleast public. But I don't want the base class to over
    > ride it. I believ in Java I can put 'final' as a modifier. Is there an
    > equiv in C++, besides a comment like this:
    > //over-riding this function will result in a pink-slip.
    >
    > Thanks,
    >
    > ~S


    Unless SaveDataWithWarn is virtual, it can't be overridden.
    Or are you asking something else? I lack a clear picture.

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
     
    Tobias Blomkvist, Jul 25, 2005
    #2
    1. Advertising

  3. Shea Martin

    Shea Martin Guest

    Tobias Blomkvist wrote:
    > Shea Martin sade:
    >
    >> I have a pure virtual f'n bool SaveData(). This f'n is called by
    >> SaveDataWithWarn(). The idea being the implementing class must define
    >> how data is saved, but the base class handles GUI operations, and user
    >> interaction.
    >>
    >> SaveDataWithWarn is called by a GUI event trigger. But it is possible
    >> that an implementing class would like to manually call this function.
    >> Thus it must be atleast public. But I don't want the base class to
    >> over ride it. I believ in Java I can put 'final' as a modifier. Is
    >> there an equiv in C++, besides a comment like this:
    >> //over-riding this function will result in a pink-slip.
    >>
    >> Thanks,
    >>
    >> ~S

    >
    >
    > Unless SaveDataWithWarn is virtual, it can't be overridden.
    > Or are you asking something else? I lack a clear picture.
    >
    > Tobias


    Sure it can. Try this:

    class A
    {
    void fn()
    {
    std::cout << "A::fn()\n";
    }
    }

    class B : A
    {
    void fn()
    {
    //something different.
    std::cout << "B::fn()\n";
    }
    }

    Virtual just specifies which function gets called when polymorphism is
    in action. For example:

    B *b = new B();
    A *a = 0;

    b->fn(); //prints B::fn()
    a = b;
    a->fn(); //prints A::fn(), because fn() is not virtual
    //if fn() was declared virtual in class A,
    //then B::fn() would be printed.

    ~S
     
    Shea Martin, Jul 25, 2005
    #3
  4. Shea Martin sade:
    >
    > Virtual just specifies which function gets called when polymorphism is
    > in action. For example:
    >


    Exactly.

    > B *b = new B();
    > A *a = 0;
    >
    > b->fn(); //prints B::fn()
    > a = b;
    > a->fn(); //prints A::fn(), because fn() is not virtual
    > //if fn() was declared virtual in class A,
    > //then B::fn() would be printed.
    >
    > ~S


    Since you mentioned a pure virtual function, I assume your question is
    based on polymorphism, and if you omit virtual from SaveDataWithWarn
    the derived class can't override it from the polymorphic perspective,
    since the base class pointer will always call the base class function.

    But as I said, I lack a clear picture; since I can't really follow
    the connections within your question.

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
     
    Tobias Blomkvist, Jul 25, 2005
    #4
  5. Shea Martin

    dragoncoder Guest

    > Sure it can. Try this:

    No it can't.

    > class A
    > {
    > void fn()
    > {
    > std::cout << "A::fn()\n";
    > }
    >
    > }


    > class B : A
    > {
    > void fn()
    > {
    > //something different.
    > std::cout << "B::fn()\n";
    > }
    >
    > }

    What you are talking about, is called overloading and not overriding.

    > Virtual just specifies which function > gets called when polymorphism is
    > in action. For example:
    >
    > B *b = new B();
    > A *a = 0;
    >
    > b->fn(); //prints B::fn()
    > a = b;
    > a->fn(); //prints A::fn(), because fn() is > not virtual
    > //if fn() was declared virtual in > class A,
    > //then B::fn() would > be printed.

    Thats because fn() would be overriden.

    The term "overriding" comes under the context of polymorphysm, which is
    implemented using virtual keyword.
     
    dragoncoder, Jul 25, 2005
    #5
  6. On Mon, 25 Jul 2005 14:59:57 GMT, Shea Martin <>
    wrote:

    >Virtual just specifies which function gets called when polymorphism is
    >in action. For example:


    Generally speaking, it is stylistically inappropriate to do this sort
    of overriding when dealing with polymorphism. In C++, the 'class'
    mechanism is reused for several different purposes (concrete, abstract
    bases, mixin implementation, vanilla polymorphic, trait, etc) for each
    of which certain features are inappropriate to use. You can contrast
    this to Java, where classes are user-defined polymorphic types, and
    pretty much nothing else.

    It's worth pointing out that this non-virtual override is not the same
    function as it's parent's function; it just happens to share the same
    signature.

    Used from the base class type, the object will still operate correctly
    and as expected. The only trouble here is if a user is using the
    object as the derived type, without consulting the class
    documentation, and expecting it to work properly. But this is the
    same with non-virtual overriding or no: blindly using an interface
    without consulting the documentation may result in strange things.
    Besides, why are they using the derived type if they want guarantees
    provided only by the parents?

    Since this sort of thing is a misuse anyway, why do you care?

    Many compilers can be configured to give a warning for this situation.
    It's too bad that many low quality textbooks (and low quality
    universities) continue to teach non-virtual overriding, often eliding
    discussion of 'virtual' itself entirely.

    But no, to answer your question, this is not possible without doing
    something very invasive, such as parameterizing the base class with
    the type of the derived.

    Aaron W. LaFramboise
     
    Aaron W. LaFramboise, Jul 25, 2005
    #6
  7. Shea Martin

    Shea Martin Guest

    dragoncoder wrote:
    >>Sure it can. Try this:

    >
    >
    > No it can't.
    >
    >
    >>class A
    >>{
    >> void fn()
    >> {
    >> std::cout << "A::fn()\n";
    >> }
    >>
    >>}

    >
    >
    >>class B : A
    >>{
    >> void fn()
    >> {
    >> //something different.
    >> std::cout << "B::fn()\n";
    >> }
    >>
    >>}

    >
    > What you are talking about, is called overloading and not overriding.

    Oh, I though overloading was having multiple versions of a function,
    each version taking different arguments.
    see the following examples of what some people call overloading:
    http://msdn.microsoft.com/library/d...clang/html/_pluslang_function_overloading.asp
    or
    http://www.maxwell.ph.kcl.ac.uk/~courses/cp4731/C8.html#fo
    or
    http://valis.cs.uiuc.edu/~sariel/teach/1998/soft99a/tirgul/99_02_24/99_02_24.html

    Maybe you should ask them what they are talking about.

    ~S
     
    Shea Martin, Jul 26, 2005
    #7
  8. Shea Martin

    Shea Martin Guest

    Tobias Blomkvist wrote:
    > Since you mentioned a pure virtual function, I assume your question is
    > based on polymorphism, and if you omit virtual from SaveDataWithWarn
    > the derived class can't override it from the polymorphic perspective,
    > since the base class pointer will always call the base class function.
    >


    Sorry, I guess I should have been more clear. SaveDataWithWarn was the
    call I was wanting to prevent an implementing class from overriding.

    I guess in a sense you are right, if it is not virtual, then my version
    (version defined in base class) will always be called.... but if a
    downcast is performed, then it would be possible to get the implementing
    classes version. Though your solution is still fairly effective.

    The solution is still a runtime solution, unlike java's "final", which
    is a compile time enforcement. Anyway I have my anser now.

    Cheers,

    ~S
     
    Shea Martin, Jul 26, 2005
    #8
  9. Shea Martin

    Shea Martin Guest

    Aaron W. LaFramboise wrote:
    > Since this sort of thing is a misuse anyway, why do you care?

    Good point. Sometimes I get sidetracked...

    > Many compilers can be configured to give a warning for this situation.

    A 'W' directive... Wall should work.

    > It's too bad that many low quality textbooks (and low quality
    > universities) continue to teach non-virtual overriding

    Very true.


    > But no, to answer your question, this is not possible without doing
    > something very invasive, such as parameterizing the base class with
    > the type of the derived.


    Thanks for a great answer.


    ~S
     
    Shea Martin, Jul 26, 2005
    #9
    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. JFCM
    Replies:
    4
    Views:
    5,788
  2. Oliver Wong
    Replies:
    14
    Views:
    1,681
    Chris Uppal
    Jun 13, 2006
  3. Anthony Baxter
    Replies:
    0
    Views:
    355
    Anthony Baxter
    Sep 3, 2004
  4. Replies:
    5
    Views:
    546
    Chris Uppal
    Nov 17, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    387
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page