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. Calling child class methods from parent class pointers

Calling child class methods from parent class pointers

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    What I am trying to do is create a base class that provides a basic interface and several child classes that implement the interface and extend it a little bit. I would then like to create a child class (at run time using pointers to the base class) whose type is determined by data in a table. So far so good. In the code which has the pointer I am trying to call some of the functions that extend the interface depending on the situation. Compiler chokes (rightly) telling me the virtual base class doesn't implement those functions. I can get around this by making the virtual base class define all the possible functions in the child classes and put dummy implementations in some child classes but it seems in-elegant. Is there a better way to do this?

    _ 1 Reply Last reply
    0
    • M mjackson11

      What I am trying to do is create a base class that provides a basic interface and several child classes that implement the interface and extend it a little bit. I would then like to create a child class (at run time using pointers to the base class) whose type is determined by data in a table. So far so good. In the code which has the pointer I am trying to call some of the functions that extend the interface depending on the situation. Compiler chokes (rightly) telling me the virtual base class doesn't implement those functions. I can get around this by making the virtual base class define all the possible functions in the child classes and put dummy implementations in some child classes but it seems in-elegant. Is there a better way to do this?

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Since you are using the base class pointer to call the functions, those functions must exist as virtual or pure virtual in the base class. In the derived classes, you must override whatever virtual functions are needed. In the case of pure virtual functions, it is mandatory that you implement this in the derived classes. So instead of having dummy implementations is several derived classes, have a dummy implementation in the base class and don't make it pure virtual.

      «_Superman_» I love work. It gives me something to do between weekends.

      M 1 Reply Last reply
      0
      • _ _Superman_

        Since you are using the base class pointer to call the functions, those functions must exist as virtual or pure virtual in the base class. In the derived classes, you must override whatever virtual functions are needed. In the case of pure virtual functions, it is mandatory that you implement this in the derived classes. So instead of having dummy implementations is several derived classes, have a dummy implementation in the base class and don't make it pure virtual.

        «_Superman_» I love work. It gives me something to do between weekends.

        M Offline
        M Offline
        mjackson11
        wrote on last edited by
        #3

        I did it by adding the virtual functions to the base class. Just seems like a clunky solution. Thx very much.

        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