Call no-virtual subclass method from a pointer base abstract class

TimeToEnjoy

I need to use a pointer of class A. But how can I call the class B method in this way? The method is not virtual, it owns at class B.

class A
{
private:
    string x;
public:
    virtual void J() = 0;
};

class B : public A
{
private:
    int y;
public:
    virtual void J(){
        cout << "J()";
    }
    void K(){
        cout << "K()";
    }
};

int main(){
    B b;
    A* a = &b;
    K(); //How can I call method K() with pointer a?
}
Jarod42

With dynamic_cast, you may do

B b;
A* a = &b;
if (auto* bp = dynamic_cast<B*>(a)) {
     bp->K();
}

but you should probably rethink your design.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call base virtual method by pointer to function from derived class

From Dev

Java: calling subclass implementation of abstract base class method from base class itself but with subclass instance

From Dev

Call base class from a subclass

From Dev

Inheriting a virtual class method - how to call it from base class?

From Dev

Call derived class method from base pointer list loop (OOD)

From Dev

Call through the base class abstract method

From Dev

Override virtual method from abstract class

From

Does delete on a pointer to a subclass call the base class destructor?

From Dev

Is it acceptable to return a canonical representation subclass from an abstract base class?

From Java

How to call abstract method from abstract class called by inherit class

From Dev

Is there a way to call derived class functions with base class pointer without virtual

From Dev

Unable to call abstract class method from interface

From Dev

Call a method from abstract class c#

From Java

How to mock protected subclass method inherited from abstract class?

From Dev

How to automatically call a method or generate code if a subclass derived from a base class?

From Dev

Force base class virtual method call inside of base class

From Dev

Call base method from inherited function pointer

From Dev

Refering to subclass from abstract class

From Dev

How to call subclass method via base class without casting

From Dev

c++ Call subclass method in vector of base class type

From Dev

C++11 Polymorphism : Call derived class method from base class pointer

From Dev

Call base method from the base class

From Dev

Is It possible to call methods from a subclass from a base class type?

From Dev

Why does the base class pointer point to the pure virtual method in the base class instead of the overidden method in the derived class?

From Dev

How to override static abstract method of base in a subclass?

From Java

How to force subclass to call an abstract implemented method

From Dev

Is there a way to call method pointer from any class

From Dev

Is there a way to call method with base class pointer that uses the data in derived class?

From Dev

Error "__init__ method from base class is not called" for an abstract class

Related Related

  1. 1

    Call base virtual method by pointer to function from derived class

  2. 2

    Java: calling subclass implementation of abstract base class method from base class itself but with subclass instance

  3. 3

    Call base class from a subclass

  4. 4

    Inheriting a virtual class method - how to call it from base class?

  5. 5

    Call derived class method from base pointer list loop (OOD)

  6. 6

    Call through the base class abstract method

  7. 7

    Override virtual method from abstract class

  8. 8

    Does delete on a pointer to a subclass call the base class destructor?

  9. 9

    Is it acceptable to return a canonical representation subclass from an abstract base class?

  10. 10

    How to call abstract method from abstract class called by inherit class

  11. 11

    Is there a way to call derived class functions with base class pointer without virtual

  12. 12

    Unable to call abstract class method from interface

  13. 13

    Call a method from abstract class c#

  14. 14

    How to mock protected subclass method inherited from abstract class?

  15. 15

    How to automatically call a method or generate code if a subclass derived from a base class?

  16. 16

    Force base class virtual method call inside of base class

  17. 17

    Call base method from inherited function pointer

  18. 18

    Refering to subclass from abstract class

  19. 19

    How to call subclass method via base class without casting

  20. 20

    c++ Call subclass method in vector of base class type

  21. 21

    C++11 Polymorphism : Call derived class method from base class pointer

  22. 22

    Call base method from the base class

  23. 23

    Is It possible to call methods from a subclass from a base class type?

  24. 24

    Why does the base class pointer point to the pure virtual method in the base class instead of the overidden method in the derived class?

  25. 25

    How to override static abstract method of base in a subclass?

  26. 26

    How to force subclass to call an abstract implemented method

  27. 27

    Is there a way to call method pointer from any class

  28. 28

    Is there a way to call method with base class pointer that uses the data in derived class?

  29. 29

    Error "__init__ method from base class is not called" for an abstract class

HotTag

Archive