Jump to content


Photo

Interface Design Pattern Problems


No replies to this topic

#1 Chojun

Chojun

    Newbrie

  • Members
  • 2 posts

Posted 22 October 2010 - 05:38 AM

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!



Reply to this topic



  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users