0

if you declared/defined a virtual function for a class and redefined it in a derived class so that when you call the derived class it calls its own version of the function and not the base class function , how do you call the base class function on the definition of the derived class?

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by Fbody
0

I think this is what you want

#include <iostream>

class m1
{
public:
  
  virtual void display_it(void) const { std::cout << "from m1" << std::endl; }
  
};

class m2:public m1
{
public:
  
  void display_it(void) const { std::cout << "from m2" << std::endl; }
  
};

int main(int argc, char** argv)
{
    m2 me;
    
    me.m1::display_it();
    return 0;
}
0

This smacks of a design issue and begs a few questions:
If you're not going to use it,why do you have the derived version of the function at all?
What is so different about the "m2" version that you feel you need to call the "m1" version instead.
Are the 2 classes actually even related?
Should this be a composition design instead of an inheritance design?

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.