inheritance in OFW

Hi

I’d like to be able to extend testApp within my own class, so that my class can listen for mouseEvents.

An example of c++ inheritance I’ve seen online looks like:

**class vehicle{
public:
vehicle();
}

class car: public vehicle{
public:
car();
}**

How can we do this within the set-up of OFW?

Thanks

hi chris,

there is a short and a long answer to your question. I guess this kind of an advanced topic

the short answer is that you are currently doing this in the code:

  
  
class testApp : public ofSimpleApp{  
  

and that you wont be able to create multiple classes that all get mouse events the same way that testApp does, because currently ofApp runner is setup to take one ofSimpleApp and pass events to it. there is no way to do that across all objects of a certain type, unless they are “registered” in some way.

the long answer is that you could do this in code, by having a class that other classes extend that takes events, I write this is pseudo code:

  
  
class eventGetter{  
   public:  
   void mousePressed(int x, int y, int button);  
}  
  

then you can extend it:

  
  
class coolGuy : public eventGetter{  
  void sayHi(){  
   printf("too cool for school \n");  
 }  
}  
  
class otherGuy: public eventGetter{  
}  
  
class anotherOne: public eventGetter{  
}  
  

then, you can do the following in your code:

  
  
eventGetter * myEventListeners[3];   // should be a pointer, because we will use polymorphism to our advantage....  
  
// ----------------------------------------------  
myEventListeners[0] = new coolGuy ();  // tricky!!!!  we can allocate to the inheriter class....  
myEventListeners[1] = new otherGuy();  
myEventListeners[2] = new anotherOne();  
  
  
  
// ----------------------------------------------  
void mousePressed(...){  
myEventListeners[0]->mousePressed(...)  
myEventListeners[1]->mousePressed(...)  
myEventListeners[2]->mousePressed(...)  
}  
  
// if you need to call something specific, you need to cast, so that the compiler will know   
((coolGuy * )myEventListeners[0])->sayHi(...)  
  
  

hope that helps - we are currently looking at strategies for making variables such as mouseX, etc accesible across the whole code, such like processing is when you use inner classes (other tabs named .pde)…

take care,
zach