Jump to content
Chojun

Interface Design Pattern Problems

Recommended Posts

Hello,

 

My software project uses interfaces extensively because we have a fair range of hardware and configurations that we're supporting. Using interfaces cleans up our code and design considerably. The most critical piece, an event engine, uses interfaces as well. However, I'm having all kinds of problems with using interfaces in sourceboost.

 

Firstly, I'd like to point out that if you define an interface, and have a class that implements the interface but forgets to implement one of the interface methods, the compiler (or linker, can't remember which) will crash. The compiler should really catch this. Would've saved me hours of troubleshooting if it had. .. :rolleyes:

 

Second, I've come to suspect the way that SourceBoost constructs the vTable since a piece of code that should work absolutely will not. Take the following code example (please ignore syntax/semantics as I'm typing this up on the fly, but if anything stands out feel free to ask):

 

 

class IInterface
{
public:
 virtual void foo() = 0;

}

class A : public IInterface
{
public:
 void foo() { ... }

}

class B : public IInterface
{
public:
 void foo() { ... }

}

class CFactory
{
public:
 void run()
 {
IInterface *array[3];
A a;
B b;

array[0] = &a;
array[1] = &b;
array[2] = 0;

processClasses(array);

 }

 void processClasses(IInterface **array)
 {
int i = 0;
while (array[i] != 0)
  array[i++]->foo();  //!!! Locks up here !!!

 }

}

 

With the program locking up on the call to the interface method, it causes me to wonder if something is wrong with sourceboost's vTable implementation. If I were to do something like this instead:

 

(A *)array[0]->foo();

 

..then it works great, but defeats the purpose of interfaces and throws our whole design out the window. Anyone have any insight into the problem I'm experiencing?

 

 

Thank you very much in advance!

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×