Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 49
Registered: ‎03-27-2013
My Device: z10
My Carrier: Banglalink

C++ design issue about inheritance

Let me describe the problem-

 

Base class-

class Parent

{

....

void funX() = 0;

....

};

 

Derived class-

class Child1

{

.....

void funX();

.....

}

 

void Child1::funX()

{

....

....

}

 

Derived class2-

class Child2

{

.....

void funX();

.....

}

 

void Child2::funX()

{

// Empty function body

}

 

From another class we use parent class pointer to execute derived classes funX()-

vector <Parent *> derivedClasses;

for (int i = 0; i < derivedClasses.size(); i++)

{

    derivedClasses[i]->funX();

}

 

As you can see derived Child2 implement funX() with empty body, because it's not needed for Child2. We all know that there is a cost of invoking a method and go out of it. So, I want to know what will be the best design principle to handle this kind of situation?

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: C++ design issue about inheritance

I suppose the function is declared as virtual in Parent. There's a small overhead of calling virtual functions because Virtual Method Table lookup is required. And you can't "hide" the function in Child2. I wouldn't worry about this unless the class is used in a tight render loop or very complex computations. Otherwise avoiding inheritance with virtual functions could save a few CPU cycles.

Andrey Fidrya, @zmeyc on twitter