C++ and inheritance in abstract classes

12,799

Solution 1

thanks for the answers.. helped me a lot to understand the problem. In effect I posted some wrong code, because i was misunderstanding the real problem. Anyway, i think i partially solved my problem. Here's the code:

 #include <iostream>

``  using namespace std;

class Other{

public:

void foo(){
        cout << "Other\n";
}

void foo(int a){}
};

class Abstract : public Other{

public:
virtual void foo() {};
virtual void foo(int c){
        Other::foo(c);
}
};

class A : public Abstract{

public:

void foo(){
        cout << "A\n";
}
};

class B : public Abstract{

public:

void foo(){
        cout << "B\n";
}
};
int main(){

cout << "main\n";

Abstract * ptrA = new A();
Abstract * ptrB = new B();

Other *o = new Other();
o->foo();

ptrA->foo();
ptrB->foo();
ptrB->foo(3); //can't no more use the method foo with different signatures implemented in the base class Other, unless I explicitly redefined in the class Abstract
dynamic_cast<Other*>(ptrB)->foo(3);//can't dynamic_cast from derived to base

I was making two errors:

  1. In my real code (not the simplified version posted before) i forgot to declare virtual the function foo()

  2. Even declaring virtual wasn't enough. In fact all the implementations of that function must be wrapped inside the class Abstract to become visible to the subclasses A and b. Otherwise wont't compile.

I don't know if it could be a clean solution..in fact that way I need to wrap all foo method signatures.

Solution 2

Why don't you do:

class OtherClass 
{
    public:
    virtual void anyMethod()
    {
       cout << "OtherClass";
    };
}

That should solve your problems

Solution 3

I think that if the method in OtherClass that you want to override in A and B is NOT virtual, then the override is not implicit.

I believe there's a way to Explicitly override the functions, look that up.

Solution 4

If anyMethod was declared virtual in the base class to which you have a pointer or reference, it should be looked up virtually and print A and B correctly. If it wasn't, then there is nothing you can do (beyond changing it to be virtual).

Solution 5

As far as I can see from your code OtherClass::anyMethod() is not a virtual and already implemented. It should work as you described if you define it as virtual

Share:
12,799
Heisenbug
Author by

Heisenbug

Updated on June 15, 2022

Comments

  • Heisenbug
    Heisenbug almost 2 years

    i have a problem in properly handling method overriding where an abstract class is present inside my classes hierarchy. I'll try to explain:

    class AbstractClass{
    public:
        virtual void anyMethod() = 0;
    };
    
    class A : public AbstractClass {
        void anyMethod() {
            // A implementation of anyMethod
            cout << "A";
        }
    };
    
    class B : public AbstractClass {
        void anyMethod() {
            // B implementation of anyMethod
            cout << "B";
        }
    };
    
    AbstractClass *ptrA, *ptrB;
    
    ptrA = new A();
    ptrB = new B();
    ptrA->anyMethod();  //prints A
    ptrB->anyMethod();  //prints B
    

    Ok..previous example work fine .. the concrete implementation of the AbstractClass method anyMethod will be called at run time. But AbstractClass is derived from another base class which has a method not virtual called anyMethod:

    class OtherClass {
    public:
        void anyMethod() {
            cout << "OtherClass";
        }
    };
    
    class AbstractClass : public OtherClass {
    public:
        virtual void anyMethod() = 0;
    };
    
    //A and B declared the same way as described before.
    

    Now , if i try something like that:

    ptrA = new A();
    ptrB = new B();
    ptrA->anyMethod();  //prints OtherClass
    ptrB->anyMethod();  //prints OtherClass
    

    What am I misunderstanding? Is there any solution for making ptrA and ptrB printing A and B without using cast, typeid, etc?

    • vitaut
      vitaut over 13 years
      A and B should be printed in the second case as well. Are you missing something from your description?
    • casablanca
      casablanca over 13 years
      I tested your code on g++ and it prints A and B in both cases. Are you using OtherClass * by any chance?
    • Elalfer
      Elalfer over 13 years
      class AbstractClass : public Other class should be class AbstractClass : public OtherClass
    • Admin
      Admin over 13 years
      How do you declare ptrA and ptrB in the second case? The behavior only makes sense if you have OtherClass *ptrA there.
  • Heisenbug
    Heisenbug over 13 years
    Exactly.. OtherClass is in a thirdy party library that i cannot modify the signature. I noticed that i was missing to declare virtual anyFunction in AbstractClass (sorry but what i posted is a very simplified version of my code.) . Now i tryed that solution, but the problem is that in OtherClass anyFunction has several different signatures..it seems to create some conflicts if i don't redeclare all of it in AbstractClass. I'll make some tests and post a more detailed question later.. Thanks to all