Hello,
I'm trying to do something like this:
- There're Base class and Derived classes.
- Some Derived classes use multiple inheritance, from Base class and a pure virtual class called Interface. These classes are able of some functionality that ordinary Derived classes aren't.

Why doesn't it work to cast a enhanced Derived class as a Interface class, and invoke its methods??

The example below shows exactly what I mean:

int main(void)
{
   Base       *base     = new Base();
   Base       *polymorph= new Derived();
   Derived   *derived   = new Derived();

   cout << " This block works as expected. No problem" << endl << endl;

	base->methodBase();
	polymorph->methodBase();
	derived->methodBase();
	derived->methodInterface();

   cout << " The problem begins here" << endl << endl;

	((Interface*)polymorph)->methodInterface();   // <--- Problem is here
        ((Derived*)polymorph)->methodInterface();

	delete base;
	delete derived;
	delete polymorph;

	return EXIT_SUCCESS;
}

((Interface*)polymorph)->methodInterface() does not call methodInterface(), but Derived destructor. Which, of course, makes program crash on the next line, as Derived object has been destroyed.

I need to cast to Interface, because in my real application I have a list of pointers to Base class, and when I find out that some object is indeed, inheriting from Interface, I can think of no other way to call to Interface methods, without casting to every particular enhanced Derived class.
I use GNU g++.

I don't know why the above code doesn't work.
You might think I come from Java, and you'll be right. I don't use Java anymore, since some years ago, but this is the way I would work on it, using interface classes.
I'm sure there must be another way to go in C++, but I usually like my code to be as much portable as possible, and tend to stay away from language too-much-specific features.

I'd appreciate ANY comment, help, recommendation.

Thank you!

Full source in one single file:

#include <iostream>
using namespace std;

class Base
{
public:
               Base() {};
   virtual    ~Base() {};
   virtual void methodBase(void)
   {
      cout << " Base::methodBase" << endl;
   }
};

class Interface
{
public:
   /*
            Interface();
   virtual ~Interface();
    */
   virtual void   methodInterface( void ) = 0;
};


class Derived : public Base, public Interface
{
public:
            Derived() {};
   virtual ~Derived() {};

   virtual void methodInterface( void )
   {
      cout << " Derived::methodInterface" << endl;
   }

   void methodBase( void )
   {
      cout << " Derived::methodBase" << endl;
   }

};



int main(void)
{
   Base       *base     = new Base();
   Base       *polymorph= new Derived();
   Derived    *derived  = new Derived();
   Interface  *inter;

	cout << " cplus_interfaces.cpp" << endl << endl;

	base->methodBase();
	polymorph->methodBase();
	derived->methodBase();
	derived->methodInterface();

	cout << " The problem begins here" << endl << endl;

	((Interface*)polymorph)->methodInterface();
   ((Derived*)polymorph)->methodInterface();

	delete base;
	delete derived;
	delete polymorph;

	return EXIT_SUCCESS;
}
Be a part of the DaniWeb community

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