I'm creating an interface for my project and curious when the idea may come true or otherwise. This is actually the situation, At run time, I wish to use a range of base class pointers to problem instructions to various derived objects. Different derived objects got different implementation (virtual functions). My issue is: if individuals objects got different different support degree of the interface, how do i avoid writing a clear function?

For instance, (my current code)

Class Base { //this is the main interface
public:
   virtual void basicFun() = 0;
   virtual void funA(int input1) {return;}
   virtual void funB(int input2) {return;}
   virtual void funC(int input3) {return;}
}

Class Derived1 : public Base { //this class support only funA()
public:
   void basicFun() {//....}
   void funA(int input1) {//do something}
}

Class Derived2 : public Base { //this class support both funA() funB()
public:
   void basicFun() {//....}
   void funA(int input1) {//do something}
   void funB(int input2) {//do something}
}

Class Derived3 : public Base { //this class support all
public:
   void basicFun() {//....}
   void funA(int input1) {//do something}
   void funB(int input2) {//do something}
   void funC(int input3) {//do something}
}

Assumption: for any certain object, unsupported function would not be known as. i.e. BasePtr->funC() should never be known as when the object pointed by basePtr is Derived1 or Derived2. The issue is:

  1. I have to define a clear function in both Base or Derived if the uniform interface is preferred
  2. If empty functions are defined like above, compiler keeps warning me unreferenced parameters (input1~input3). Obviously I'm able to power it down, but simply can't stand by doing this.

So, can there be any pattern will be able to use to attain an uniform interface without determining empty functions? I've been considering this for any couple of days. It appears impossible. Because funA() funB() and funC() should be within the interface to ensure that I'm able to make use of a Base pointer array to manage all objects, meaning in Derived1, funB() and funC() must in some way be defined.

Thanks, and happy thanksgiving, and thank you for discussing your opinions.

Solti

Uniform connects really are a positive thing. Meaning you have to implement all techniques within an interface, even when this means you'll have empty techniques. There's no design pattern with this problem, since it is not really a real problem to begin with.

Consider this as it were: if you have a Car interface, with techniques Accelerate() and Brake(). A category that derives from Car must implement all techniques. Can you want an item based on Car implement the Accelerate() method although not the Brake() method? It might be an incredibly unsafe Car!

An interface poor OOP should have a properly defined contract that's stuck to by each side. In C++, this really is enforced to some extent by needing all pure virtuals to become implemented in derived class(es). Attempting to instantiate a category with unimplemented virtual techniques lead to compilation error(s), presuming one does not use stupid methods to obtain around it.

You resist creating empty techniques simply because they cause compiler alerts. Inside your situation, just omit the parameter title:

void funC(int) // Note lack of parameter name
{
}

Or comment the title out:

void funC(int /*input3*/)
{
}

As well as by using templates!

template<class T> void ignore( const T& ) { }

//...

void funC(int input3)
{
    ignore(input3);
}