Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Virtual Function Question

Virtual Function Question

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TFrancis
    wrote on last edited by
    #1

    I'm actually using WTL in this project, but this appears to be a basic C++ question so I thought I'd ask it here. I have class A, which is a base class for class B. Class B is in turn a base class for class C. In class A there is a virtual function. Let's call it Func1(). This function is defined in both class A and C (the A version just asserts, the C version actually does the work). In class B there is a non-virtual function that calls Func1(). As I understand it, it should call the class C version of this function, but instead it's calling the class A version. Where have I gone wrong?

    R T 2 Replies Last reply
    0
    • T TFrancis

      I'm actually using WTL in this project, but this appears to be a basic C++ question so I thought I'd ask it here. I have class A, which is a base class for class B. Class B is in turn a base class for class C. In class A there is a virtual function. Let's call it Func1(). This function is defined in both class A and C (the A version just asserts, the C version actually does the work). In class B there is a non-virtual function that calls Func1(). As I understand it, it should call the class C version of this function, but instead it's calling the class A version. Where have I gone wrong?

      R Offline
      R Offline
      rpadrela
      wrote on last edited by
      #2

      because class B is derived from class A. If you don't override Func1() in class B, Func1() from class A will be called because inherits Func1() from it's base class( class A ). Do not forget that class B is not related with class C. The relationship u described is from C to B and not from B to C.

      1 Reply Last reply
      0
      • T TFrancis

        I'm actually using WTL in this project, but this appears to be a basic C++ question so I thought I'd ask it here. I have class A, which is a base class for class B. Class B is in turn a base class for class C. In class A there is a virtual function. Let's call it Func1(). This function is defined in both class A and C (the A version just asserts, the C version actually does the work). In class B there is a non-virtual function that calls Func1(). As I understand it, it should call the class C version of this function, but instead it's calling the class A version. Where have I gone wrong?

        T Offline
        T Offline
        TFrancis
        wrote on last edited by
        #3

        Just FYI, this really does work, and is infact the basis of the template design pattern. Where I went wrong: class A was written by somebody else (in this case it was part of WTL written by Microsoft). I thought it was virtual, it wasn't. If it was virtual it would've worked, as I've now found out. Why it works: The method in class B that calls the virtual function is calling it on the this pointer. Thus, the virtual table is used to figure out which version to call, so even though class B doesn't know about class C, class C's definition of the function is used. Pretty cool when you do it right.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups