|
-
October 30th, 2010, 02:51 PM
#14
Re: Override different part of member functions in different derived classes
This solution works but it is ludicrously complicated.
There is one base class and one derived class. The base class has all the default behaviour and the derived class has all the specialised versions of the base class' overridable functions. You override a base class function on a specific instance of the derived class by calling the corresponding setFuncX function and passing it a pointer to the specialised function. See the MemberfunctionDemo() function.
To be honest I think it would be simpler to just invent lots of derived names and live with the small task of writing the code which calls the base class, but it has been fun to come up with this solution.
Code:
#include <iostream>
class Base
{
public:
Base(int i, int j)
: x(i), y(j)
{
}
virtual ~Base() {}
// Public functions.
inline void funcA(char arg){ funcImpA(arg); }
inline void funcB(long arg){ funcImpB(arg); }
inline void funcC(float arg){ funcImpC(arg); }
protected:
// The base class' member functions:
virtual void funcImpA(char arg){ std::cout << "Base::funcImpA: " << arg << std::endl; }
virtual void funcImpB(long arg){ std::cout << "Base::funcImpB: " << arg << std::endl; }
virtual void funcImpC(float arg){ std::cout << "Base::funcImpC: " << arg << std::endl; }
private:
int x;
int y;
};
class Derived : public Base
{
public:
Derived(int i, int j):Base(i,j)
{
funcPtrA=&Derived::funcDefA;
funcPtrB=&Derived::funcDefB;
funcPtrC=&Derived::funcDefC;
}
// typedefs of the member functions
typedef void (Derived::*FunctionA)(char arg);
typedef void (Derived::*FunctionB)(long arg);
typedef void (Derived::*FunctionC)(float arg);
void setFuncA(FunctionA func){funcPtrA = func;}
void setFuncB(FunctionB func){funcPtrB = func;}
void setFuncC(FunctionC func){funcPtrC = func;}
// Default behaviour.
void funcDefA(char arg){ Base::funcImpA(arg); }
void funcDefB(long arg){ Base::funcImpB(arg); }
void funcDefC(float arg){ Base::funcImpC(arg); }
// The derived class' member functions:
void funcImpB1(long arg){ std::cout << "Derived::funcImpB1: " << arg << std::endl; }
void funcImpB2(long arg){ std::cout << "Derived::funcImpB2: " << arg << std::endl; }
protected:
virtual void funcImpA(char arg){ (this->*funcPtrA)(arg); }
virtual void funcImpB(long arg){ (this->*funcPtrB)(arg); }
virtual void funcImpC(float arg){ (this->*funcPtrC)(arg); }
private:
// The pointers to the member functions.
FunctionA funcPtrA;
FunctionB funcPtrB;
FunctionC funcPtrC;
};
void MemberfunctionDemo()
{
Derived original(1,2);
Derived someobj(3,4);
Derived another(5,6);
someobj.setFuncB(&Derived::funcImpB1);
another.setFuncB(&Derived::funcImpB2);
original.funcA('a');
original.funcB(1);
original.funcC(1.0);
someobj.funcA('b');
someobj.funcB(2);
someobj.funcC(2.0);
another.funcA('c');
another.funcB(3);
another.funcC(3.0);
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|