How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

aaroh

We can't create an object of an abstract class, right? So how can I call a virtual function which has definition in both abstract base class and derived class? I want to execute the code in abstract base class but currently, I am using derived class's object.

class T
{
   public:
   virtual int f1()=0;
   virtual int f2() { a = 5; return a }
}
class DT : public T
{
   public:
   int f1() { return 10; }
   int f2() { return 4; }
}

int main()
{
    T *t;
    t = new DT();
    ............
}

Is there any way I can call the base class function using the object t? If it is not possible, what should I need to do to call the base class function?

Angew is no longer proud of SO

Just like you would call any other base class implementation: use explicit qualification to bypass the dynamic dispatch mechanism.

struct AbstractBase
{
  virtual void abstract() = 0;

  virtual bool concrete() { return false; }
};

struct Derived : AbstractBase
{
  void abstract() override {}

  bool concrete() override { return true; }
};

int main()
{
  // Use through object:
  Derived d;
  bool b = d.AbstractBase::concrete();
  assert(!b);

  // Use through pointer:
  AbstractBase *a = new Derived();
  b = a->AbstractBase::concrete();
  assert(!b);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I friend a derived class function in the base class?

From Dev

Abstract base class definition

From Dev

How to call a function from a derived class in a base class?

From Dev

How a friend function of a base class is calling the overridden virtual function of derived class even though they are private ?(C++)

From Dev

C++11: Can I explicitly call a base class destructor to destroy the derived class?

From Dev

How can I have an object of a derived class "reference" members of an instance of the base class?

From Dev

Call base() from derived class to execute base class function?

From Dev

C++ Abstract Base Class with pure virtual operator+ function

From Dev

Virtual function definition in base class gives error

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Can't enforce the use of base constructor of an abstract class into derived class

From Dev

How can a derived class pointer to a base class object call methods of the derived class?

From Dev

How can a derived class pointer to a base class object call methods of the derived class?

From Dev

How can I define asp.net Htmlhelper extensions for derived classes of an abstract base class?

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

Can I call a derived method from base class?

From Dev

Why do abstract derived classes need to initialize a virtual base class?

From Dev

Why do abstract derived classes need to initialize a virtual base class?

From Dev

C++ call derived function from base class

From Dev

Declaring pure virtual function in base class with derived class object as arguments

From Dev

Accessing virtual base class function from a derived class

From Dev

Reimplement a virtual function from base class in a derived template class

From Dev

Declaring pure virtual function in base class with derived class object as arguments

From Dev

How to pass a derived class to a base class function and return the derived class c#

From Dev

C++ Base class pointer pointing to derived class object doesn't call derived equal operator function

From Dev

In C++ public heritance Base Class call Derived class's private virtual fuction

From Dev

virtual function in base class

From Dev

virtual function in base class

Related Related

  1. 1

    How can I friend a derived class function in the base class?

  2. 2

    Abstract base class definition

  3. 3

    How to call a function from a derived class in a base class?

  4. 4

    How a friend function of a base class is calling the overridden virtual function of derived class even though they are private ?(C++)

  5. 5

    C++11: Can I explicitly call a base class destructor to destroy the derived class?

  6. 6

    How can I have an object of a derived class "reference" members of an instance of the base class?

  7. 7

    Call base() from derived class to execute base class function?

  8. 8

    C++ Abstract Base Class with pure virtual operator+ function

  9. 9

    Virtual function definition in base class gives error

  10. 10

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  11. 11

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  12. 12

    Can't enforce the use of base constructor of an abstract class into derived class

  13. 13

    How can a derived class pointer to a base class object call methods of the derived class?

  14. 14

    How can a derived class pointer to a base class object call methods of the derived class?

  15. 15

    How can I define asp.net Htmlhelper extensions for derived classes of an abstract base class?

  16. 16

    How do I call a derived class method from the base class constructor in C++?

  17. 17

    Can I call a derived method from base class?

  18. 18

    Why do abstract derived classes need to initialize a virtual base class?

  19. 19

    Why do abstract derived classes need to initialize a virtual base class?

  20. 20

    C++ call derived function from base class

  21. 21

    Declaring pure virtual function in base class with derived class object as arguments

  22. 22

    Accessing virtual base class function from a derived class

  23. 23

    Reimplement a virtual function from base class in a derived template class

  24. 24

    Declaring pure virtual function in base class with derived class object as arguments

  25. 25

    How to pass a derived class to a base class function and return the derived class c#

  26. 26

    C++ Base class pointer pointing to derived class object doesn't call derived equal operator function

  27. 27

    In C++ public heritance Base Class call Derived class's private virtual fuction

  28. 28

    virtual function in base class

  29. 29

    virtual function in base class

HotTag

Archive