Override a virtual member in a instance?

Discussion in 'C++' started by tony@donotspamme.ar, Aug 24, 2006.

  1. Guest

    Hi!
    A derived class can override a method in the base class it inherits for,
    and even my dog knows that. More incredibly, I know and understand it
    too.

    But, can a (of course virtual in this case) method be overriden in an
    instance instead? For example, to provide some callbacks to a class.

    Should I use virtual functions instead? How do I pass the this pointer
    when casting it from or to in any useful way seems to break every C++
    language rule? As a normal parameter? But then it doesn't really look
    so OOP anymore.

    Thanks a lot,
    Tony
    , Aug 24, 2006
    #1
    1. Advertising

  2. wrote:
    > A derived class can override a method in the base class it inherits
    > for, and even my dog knows that. More incredibly, I know and
    > understand it too.
    >
    > But, can a (of course virtual in this case) method be overriden in an
    > instance instead? For example, to provide some callbacks to a class.


    No. For that you usually store pointers to [member] functions and assign
    them different values based on the need, per instance.

    > Should I use virtual functions instead?


    Instead of what? Instead of virtual functions? Uh... Yes!.. I mean,
    no... I mean... Could you rephrase, please?

    > How do I pass the this pointer
    > when casting it from or to in any useful way seems to break every C++
    > language rule? As a normal parameter? But then it doesn't really look
    > so OOP anymore.


    Huh? I think I don't understand (where does it place me relative to
    your dog?), could you give an example in C++?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 24, 2006
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    wrote:

    >
    > Hi!
    > A derived class can override a method in the base class it inherits for,
    > and even my dog knows that. More incredibly, I know and understand it
    > too.
    >
    > But, can a (of course virtual in this case) method be overriden in an
    > instance instead? For example, to provide some callbacks to a class.
    >
    > Should I use virtual functions instead? How do I pass the this pointer
    > when casting it from or to in any useful way seems to break every C++
    > language rule? As a normal parameter? But then it doesn't really look
    > so OOP anymore.


    Virtual functions are type bound (although to the dynamic type). You can use
    a function pointer or a member variable whose type is a function object if
    you want to override the semantics of the call per instance.



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 24, 2006
    #3
    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. DJ Dev
    Replies:
    3
    Views:
    16,113
    Gandalf
    Feb 8, 2004
  2. Ruben Van Havermaet

    Override virtual member functions in Subclasses

    Ruben Van Havermaet, Aug 3, 2004, in forum: C++
    Replies:
    22
    Views:
    874
    jeffc
    Aug 4, 2004
  3. Replies:
    11
    Views:
    688
    James Kanze
    Sep 10, 2006
  4. Replies:
    7
    Views:
    586
    James Kanze
    May 2, 2007
  5. archimed7592
    Replies:
    4
    Views:
    739
    =?UTF-8?B?UGF3ZcWC?=
    May 30, 2007
Loading...

Share This Page