Question

I want to know creating an abstract class with multiple pure virtual methods in c++ is good or not?

For example in some situations like implementing session initiation protocol's dialogs we found that it can be implemented using bridge pattern. But having abstract base class for common operations lead to have a very large abstract class. Now from performance view what is the effect of using and implementing such class?

Consider we have different dialogs that each one have it's own implementation. (ex: InviteDialog,RegisterDialog,InfoDialog,...)

Applying bridge pattern :

class IIDialog{
public:
/*there are multiple pure virtual methodes at least 15*/
virtual int32_t SendResponse(ISipMessage* response) = 0;
protected:
/*there are multiple methods that use pure virtual methods*/
int32_t Send_Response(){retun SendResponse(response);}
}

class IInviteDialog : public IIDialog
{
/*Implemet virtual methods*/
}

class IRegisterationDialog : public IIDialog
{
/*implement virtual methode*/
}

other parts of implementation ignored(real implementations and abstracts for implementation that is used by above classes)

Thanks

Was it helpful?

Solution

With most modern C++ compilers you can assume that the performance overhead when constructing an object does not depend on the number of virtual methods.

And when actually calling the methods I would expect the overhead not being larger than that of any alternative implementation method.

And if these function calls do "complicated" things like network operations then you don't really need to bother about such micro-optimisations anyhow, it will be completely irrelevant.

OTHER TIPS

From a performance view, the number of virtual methods does not matter in C++ (there is a small constant price to pay for the indirection, regardless of the number of methods).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top