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. quick question on polymorphism [modified]

quick question on polymorphism [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionoop
3 Posts 3 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
    minkowski
    wrote on last edited by
    #1

    Hi, Think I have got my hair in a mess, say I have a function that accepts a base class ptr, and I want to call a function on a class type that derives from the base class type. Is it possible to just do a dynamic / static cast on it? something like void func(base* ptr) { //cast down hierachy so i can call my derived class function derived * derivedptr = dynamic_cast(ptr); //call my derived class function derivedptr->derivedfunction(); } Thanks for any information.

    modified on Thursday, November 26, 2009 8:30 AM

    C _ 2 Replies Last reply
    0
    • M minkowski

      Hi, Think I have got my hair in a mess, say I have a function that accepts a base class ptr, and I want to call a function on a class type that derives from the base class type. Is it possible to just do a dynamic / static cast on it? something like void func(base* ptr) { //cast down hierachy so i can call my derived class function derived * derivedptr = dynamic_cast(ptr); //call my derived class function derivedptr->derivedfunction(); } Thanks for any information.

      modified on Thursday, November 26, 2009 8:30 AM

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The best way to do something like that is to declare the function as virtual in the base class and let the child class override this method. That's the basic principle of polymorphism.

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • M minkowski

        Hi, Think I have got my hair in a mess, say I have a function that accepts a base class ptr, and I want to call a function on a class type that derives from the base class type. Is it possible to just do a dynamic / static cast on it? something like void func(base* ptr) { //cast down hierachy so i can call my derived class function derived * derivedptr = dynamic_cast(ptr); //call my derived class function derivedptr->derivedfunction(); } Thanks for any information.

        modified on Thursday, November 26, 2009 8:30 AM

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

        Like Cedric Moonen said, you should use virtual functions. But if you want to do what you're doing, you have to check the return of dynamic_cast for NULL.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        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