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?

Recommended Answers

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** …
Jump to Post

All 3 Replies

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;
}

Yes, thank you.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.