Click here to Skip to main content
13,736,690 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


27 bookmarked
Posted 4 Oct 2010
Licenced CPOL

Interfacing without Inheritence in C++

, 11 Oct 2010
Rate this:
Please Sign up or sign in to vote.
An alternate way to implement interface through aggregation rather than the usual way we know as inheritence


Have a problem with extensive use of pointers? Totally abhor macros? Do you find the following way of interface declaration totally despicable?

    ABSTRACT_METHOD(void, Draw, CDC*); 
    ABSTRACT_METHOD(void, EndEditing);
    ABSTRACT_METHOD(void, SetCtrlType, int); 
    ABSTRACT_METHOD(void, SetDisplayInfo, LPCTSTR pszInfo);
    ABSTRACT_METHOD(void, ResetData);
    ABSTRACT_METHOD(void, UpdateGUI); 

Then don't bother :). This article is not for you. Otherwise, please carry on..


Changing a comment in the header makes you recompile an entire project, containing nearly 1 million lines (maybe a little exaggeration) and taking almost half of your working hours - if you ever had to put up with this situation, then come onboard, let's share some ideas on how to get rid of such a disturbing effect on the mind.

Here recompilation is an indication of how tightly or loosely your classes are coupled; It gives some insight about how flexible your design is and how easily you can accommodate changes in your modules - such an addition of an extra class or deletion of an existing class.

As the GOF has described in design patterns, the more you make your classes loosely coupled, the easier it is for you to accommodate changes (one example of loose coupling can be, instead of declaring a member variable directly in the header, keep a pointer and instantiate in the CPP).

Whether you do without loose coupling or tight coupling, one thing you cannot live without in the modern world is interface, or in other words implementation of interface. We all know how we do it in C++, we'll have an abstract class containing pure virtual methods and we implement those methods in the derived classes. This means, even a single line change in the header of an abstract class (interface), even if it is a comment ends up compiling a lot of classes derived from it. Although I am talking about interfaces, sometimes people who are programming in C++ make their life a bit easier by declaring some member variables in the abstract classes which are supposed to be interfaces. I cannot comment whether it's good or bad, but sometimes due to short deadlines, we have to do it (giving up to the dreaded multiple inheritance - the horror in modern programming paradigm). So, in these cases even if a member variable name is changed, or new members are added, you still end up compiling a lot of classes which inherited from these abstract classes.

Those of us, who are not running a super computer, need a solution for this in big projects. Let me again re-iterate: recompilation problem solving is not the main objective here, the main objective is to achieve loose coupling and very light level of dependencies among classes.

So How Do We Plan to Meet Our Objective?

First of all, let me tell you - even I, myself, wouldn't be using these techniques I mention here, for now; at least not in my professional projects yet. Maybe, after 100 years, when(?) this article gets a noble prize or something similar for its concept, then some of you people might start using the techniques I describe here (given C++ remains the ultimate programming language even then).

As the gurus of the design world always say, aggregation is a better concept than inheritance -but what about interface implementation which is also done through multiple inheritance in C++. This is where I came up with this idea of applying the concept of aggregation in interface implementation.

Here the main concept is the user will have an interface aggregated inside the aggregator, but invoking of the interface method will end up calling the method of the aggregator class.

Speaking more visually, instead of declaring something like:

class ConcreteClassA : public AbstractClassA 
    virtual void CallFunction1();

We'd declare:

class ConcreateClassA
    AbstractClassA* m_pAbstractClassAPointer;
    void CallFunction1();

You can imagine what AbstractClassA looks like, right?

class AbstractClassA
    virtual void CallFunction1() = 0 ; // This is a pure virtual method

And somehow through a pointer of the AbstractClassA, we'd invoke the ConcreteClassA method CallClassFunction1();
Quite amazing, don't you think?. We will call the overridden virtual function of the interface AbstractClassA by a pointer of AbstractClassA type only, but that too, without inheritance. So, how?

Yes!! You guessed right. Through function pointers; to be more precise: method pointers. Now, AbstractClassA which used to look like this:

class AbstractClassA
    virtual void CallFunction1() = 0 ; // This is a pure virtual method</span />

will now look like what you see below:
(To quote a famous lyricist Bob Dylan : "It used to be like that, now it goes like this..")

class AbstractClassA
    //typedef of a method pointer; the method pointer represents a pointer type,
    //to the CallFunction1
    typedef  void</span /> (AbstractClassA::*__CallFunction1)();
    //declaration of the actual method pointer
        __CallFunction1 CallFunction1; 
    //Note: CallClassFunction1 is now a method pointer and not a pure virtual method
    //inside AbstractClassA    

Ok, now we managed to have the method pointer inside AbstractClassA but how to invoke the actual method of ConcreteClassA through it? And also, you need an object to invoke any method pointer. And this object cannot be any object, this has to be the instance of ConcreteClassA which holds AbstractClassA inside it.

So, the solution I came up with was - I would make sure my AbstractClassA is properly initialized, i.e., its function pointer has a valid value and it contains the pointer to the Concrete class in void pointer form. Unethical? This is nothing compared to instantiating the abstract AbstractClassA in the constructor of ConcreteClassA. But we are here for a noble cause, isn't it? And that is to implement interface without inheritance; so we have to play dirty a little. Just tell your fellow programmers not to directly instantiate AbstractClassA anywhere else, ok? It will only be instantiated in the constructor of those concrete classes who treat it as their interface to the outside world.

Going forward with this inventive(!) thinking, this is how we'd create and initialize an instance of AbstractClassA in the constructor of ConcreteClassA:

    AbstractClassA* q = new AbstractClassA();
    q->CallFunction1 = (AbstractClassA::__CallFunction1)(&ConcreteClassA::CallFunction1);
    m_pAbstractClassAPointer = q; 


Here the main initialization involves the assigning of the function, i.e., concrete class's method to the abstract class's method pointer and the setting of a reference of the concrete class in void pointer form inside the abstract class.

To make provision for the void pointer which is actually the address of the concrete class, we have to add methods like SetObj, GetObj and a relevant member variable in AbstractClassA making the complete picture of our abstract class looking similar to what follows:

class AbstractClassA
    //typedef of a method pointer; the method pointer represents a pointer type,
    //to the CallFunction1
    typedef  void</span /> (AbstractClassA::*__CallFunction1)();
    //declaration of the actual method pointer
        __CallFunction1 CallFunction1; 
    //Note: CallClassFunction1 is now a method pointer and not a pure virtual method
    //inside AbstractClassA

    //method to set the pointer to concrete class
    void SetObj(void* pObj){m_pObj = pObj;}
    //method to get in instance of the concrete class forcefully 
    //typecasted to abstract class
    AbstractClassA* GetObj(){ return m_pObj; }

    // pointer to the concrete class
    void* m_pObj;

And this is how we'd invoke the actual method CallFunction1 of ConcreteClassA if we have a pointer to AbstractClassA named pAbstractClassA:

( (pAbstractClassA->GetObj())->*AbstractClassA->CallFunction1 )();

Notice the clever typecasting of the void pointer to AbstractClassA* through GetObj(). This allows us to pass the this pointer of ConcreteClassA to the CallFunction1 method through AbstractClassA's method pointer. It's not a problem that we have typecasted the concrete class pointer to a completely different data type named AbstractClassA; It is due to the fact that a method pointer only requires a valid address of a proper object to be passed (as this pointer) when the method gets invoked irrespective of its type. We are casting the void pointer to AbstractClassA* only to get access to the function pointer inside it.

Since the code to invoke the method looks a bit clumsy, I came up with some useful macros for it. Also, to declare an abstract class which will always have some common methods such as GetObj(), SetObj - I came up with some more macros.

How It All Looks With the Advent of Macros

Before I start giving an example (with macros) of an interface implemented through aggregation techniques, let me talk about my interface and the implementor of the interface a little.

The name of my interface here is IDynamicCtrl. The implementor class is CDynamicCtrl which looks and acts like an edit box by default, but it's outlook and behaviour changes to a combo box when its interface method SetCtrlType is invoked. In short, IDynamicCtrl is an interface of a GUI control which can change dynamically its type in runtime.

Following is the structure of my interface IDynamicCtrl with macros:

    ABSTRACT_METHOD(void, Draw, CDC*); 
    ABSTRACT_METHOD(void, EndEditing);
    ABSTRACT_METHOD(void, SetCtrlType, int); 
    ABSTRACT_METHOD(void, SetDisplayInfo, LPCTSTR pszInfo);
    ABSTRACT_METHOD(void, ResetData);
    ABSTRACT_METHOD(void, UpdateGUI); 

I have taken all the intricacies of the interface declaration and its method pointer declarations inside the macros named ABSTRACT_CLASS, END_ABSTRACT_CLASS and ABSTRACT_METHOD.

So, to me it looks a little better and more readable with macros.

This is how the implementor class CDynamicCtrl aggregates the interface IDynamicCtrl:

// CDynamicCtrl

class CDynamicCtrl : public CStatic

    ///////////// usage of aggregation macros/////////////////

    virtual ~CDynamicCtrl(); 

    afx_msg void OnChildKillFocus();

    ///////The interface implementing methods/////////

    void Draw(CDC* pDC);
    BOOL Edit();
    BOOL IsEditing();
    void EndEditing();
    void DrawCombo(CDC* pDC);
    BOOL EditCombo();
    // nType: 0 for edit, 1 for combo 
    void SetCtrlType(int nType);
    void SetDisplayInfo(LPCTSTR pszInfo);
    BOOL AddData(LPARAM lp);
    void ResetData();
    void UpdateGUI();

    CStringArray m_arrStrings;
    BOOL m_bFocused;

    afx_msg void OnPaint();
    afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
    afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg void OnSetFocus(CWnd* pOldWnd);
    afx_msg void OnKillFocus(CWnd* pNewWnd);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

The macros required for defining the structure of an interface and its aggregation in concrete classes are declared in "abstract_class_decl.h" header of the uploaded source code.

Now, let's have a look at initialization of the interface class IDynamicCtrl and the mapping of concrete class CDynamicCtrl functions with the abstract class's function pointers (Since we are dealing with C++, I am using the terms interface class and abstract class interchangeably):




The macros related to the implementation (instantiation and method mapping) of the abstract class in concrete classes is declared in "abstract_class_impl.h" header file of the uploaded source code.

And finally and most importantly, let's have a look at the macro related to the invoking of a CDynamicCtrl method through IDynamicCtrl interface (see if it suits you, because that's the most readable version of invoking a concrete class method I could come up with):

// m_pInfoDisplayer is a saved pointer of the IDynamicCtrl interface
// which was retrieved through CDynamicCtrll::QueryIDynamicCtrl method
IDynamicCtrl* pCtrl = m_pInfoDisplayer; 
if(nViewType == 1)

If (CONCRETE(pCtrl)->SetCtrlType)(nViewType) does not suit you, you can use INVOKE(pCtrl, SetCtrlType, nViewType) macro. Both CONCRETE and INVOKE macro is defined in "abstract_class_impl.h" header file.

Background Behind the Aberration

The whole thing about implementing interface through aggregation rather than inheritance came to picture, because I work in a big project which has a lot of interface implementation through inheritance in several modules and sometimes writing comments on the header files ends up compiling the whole project. While thinking about the recompilation problem, the thought of removing one of the tightest form of coupling (bearing the name interfaces) came to mind. The article is an end result of my contemplative thinking upon inheritance and interfacing and whether it would be a nice thing to have in the C++ language itself if it really supported interfacing through aggregated classes.

Practically speaking, there were certain benefits I found when I ended up writing a sample application for this article.

Constructive Benefits from this Design

First and foremost, this concept reduces appreciably the possible ambiguity factor which might be introduced due to multiple inheritance. Like I mentioned before, working as C++ programmers under tight deadlines, you end up using the abstract class concept (which has variables) more than the interface concept (interface shouldn't have a single non static member variable). Over time, this increases ambiguity because of similar method and variable names in some of the parent classes. This technique here encourages the usage of composition of multiple objects rather than multiple inheritance and in return solves the problem related to ambiguity completely.

The other benefit is, you can change dynamically the run time behaviour of certain interface methods. For example, to give a combo look and combo behaviour to the CDynamicCtrl when the control type changes, I just remap the Draw and Edit method pointers of IDynamicCtrl to the DrawCombo and EditCombo methods of CDynamicCtrl, achieving greater flexibility and manageability in the entire process. If you'd have used inheritance instead, probably you'd end up creating another class to encapsulate different behaviours and use the correct class to display proper behaviour given the control type following the footsteps of strategy pattern; or in the worst case, you'd be using if else conditions increasing the complexity of the code - when you know very well, one of the reasons design patterns and modern techniques were introduced, were to minimize the usage of if else conditions and spaghetti codes.

The downside of this design is the way I implemented the macros, it's not possible to support polymorphic features such as function overloading as of now. However, writing the macros more cleverly and using an array of function pointers can solve this problem, but that is beyond the scope of this article.

The horror of this design, as some people may point out, is the use of void pointer, complete disregard for type safety, and breaking several ethics of OOP design; but all the hideous things mentioned above are hidden underneath the carpet of macros and the discretion of the user of macros is requested so that he may not have a peek at what's inside :).

However, like I pointed out before, if C++ supported something of this sort, the theory in practice could have been real elegant. We could ponder upon dynamic binding of an interface to a concrete class in runtime even. I know, most people are not wary of adding extra classes to encapsulate behaviour for runtime behaviour changing, but in my view if we could reduce some overheads in some cases, then why not?

During runtime, if I could write codes such as:

ISortable& sortableData = _bind(CDataTable, ISortable) // _bind is a keyword of future :)
		        {   //Sort is an ISortable interface method, and 
			   //SortByID and SortByName are CDataTable methods
			   _connect(Sort, bSortByID ? SortByID : SortByName)

And then invoke:


where The Sort function takes a reference of the ISortable interface - maybe this dynamic binding of an interface (ISortable) with a concrete class (CDataTable) could have been really useful in some cases.


  • Kaisar Ul Haque: Who helped me in many ways to understand the complex things about method pointers, the this pointer and virtual tables through his blogs.


  • 6th October, 2010: Article uploaded


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

You may also be interested in...


Comments and Discussions

QuestionThank goodnes for Lamdas Pin
Mukit, Ataul23-Apr-18 23:15
memberMukit, Ataul23-Apr-18 23:15 
GeneralEat this! Pin
mbue5-Dec-10 13:59
membermbue5-Dec-10 13:59 
GeneralRe: Eat this! Pin
Mukit, Ataul6-Dec-10 20:19
memberMukit, Ataul6-Dec-10 20:19 
GeneralOther solution like this Pin
Alkalinou12-Oct-10 7:43
memberAlkalinou12-Oct-10 7:43 
GeneralRe: Other solution like this Pin
Mukit, Ataul12-Oct-10 22:45
memberMukit, Ataul12-Oct-10 22:45 
GeneralMy vote of 2 Pin
S.H.Bouwhuis11-Oct-10 22:06
memberS.H.Bouwhuis11-Oct-10 22:06 
GeneralRe: My vote of 2 Pin
Mukit, Ataul11-Oct-10 22:49
memberMukit, Ataul11-Oct-10 22:49 
GeneralMy vote of 2 Pin
Mihai Maerean11-Oct-10 20:45
memberMihai Maerean11-Oct-10 20:45 
GeneralRe: My vote of 2 Pin
Mukit, Ataul11-Oct-10 23:09
memberMukit, Ataul11-Oct-10 23:09 
GeneralRe: My vote of 2 Pin
Aescleal12-Oct-10 2:15
memberAescleal12-Oct-10 2:15 
GeneralRe: My vote of 2 Pin
Mukit, Ataul12-Oct-10 4:26
memberMukit, Ataul12-Oct-10 4:26 
GeneralRe: My vote of 2 Pin
Aescleal12-Oct-10 9:29
memberAescleal12-Oct-10 9:29 
GeneralRe: My vote of 2 Pin
Mukit, Ataul12-Oct-10 18:24
memberMukit, Ataul12-Oct-10 18:24 
GeneralRe: My vote of 2 Pin
Aescleal13-Oct-10 3:54
memberAescleal13-Oct-10 3:54 
GeneralRe: My vote of 2 Pin
Mukit, Ataul13-Oct-10 22:22
memberMukit, Ataul13-Oct-10 22:22 
GeneralRe: My vote of 2 [modified] Pin
Aescleal14-Oct-10 2:09
memberAescleal14-Oct-10 2:09 
GeneralRe: My vote of 2 Pin
peterchen18-Mar-13 0:05
memberpeterchen18-Mar-13 0:05 
GeneralBack to C! Pin
Kaisar Haque11-Oct-10 3:14
memberKaisar Haque11-Oct-10 3:14 
GeneralRe: Back to C! Pin
Mukit, Ataul11-Oct-10 6:34
memberMukit, Ataul11-Oct-10 6:34 
GeneralMy vote of 1 Pin
Tim Craig9-Oct-10 15:17
memberTim Craig9-Oct-10 15:17 
GeneralRe: My vote of 1 [modified] Pin
Mukit, Ataul9-Oct-10 19:36
memberMukit, Ataul9-Oct-10 19:36 
GeneralMy vote of 1 Pin
Emilio Garavaglia7-Oct-10 5:34
memberEmilio Garavaglia7-Oct-10 5:34 
GeneralRe: My vote of 1 Pin
Mukit, Ataul8-Oct-10 20:56
memberMukit, Ataul8-Oct-10 20:56 
GeneralRe: My vote of 1 Pin
Emilio Garavaglia9-Oct-10 6:44
memberEmilio Garavaglia9-Oct-10 6:44 
GeneralRe: My vote of 1 Pin
Mukit, Ataul9-Oct-10 19:45
memberMukit, Ataul9-Oct-10 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 12 Oct 2010
Article Copyright 2010 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid