Hello all,
I'm working on my first flext project. I'm slowly getting the hang on things (c++ in particular) I need to create an array with the number of elements specified by the creation arguments of the object. I also need this array to be available to all functions in the class.
How to I create the array? Where do I define it? (in the constructor?) then how do I access it from another function?
Any suggestions/examples would be appreciated.
Thanks
Ben
(now that I'm in windows I realize it would have been nice to send the current code, now I can't)
Hi Ben,
I'm working on my first flext project. I'm slowly getting the hang on
things (c++ in particular) I need to create an array with the number of elements specified by the creation arguments of the object. I also need this array to be available to all functions in the class.
How to I create the array? Where do I define it? (in the constructor?)
then how do I access it from another function?
The typical solution is to put the array variable as a member into the class structure, to initialize it in the constructor and to free the memory in the destructor again. If it's a member of the class you can access it from any class member function. Of course then you'll have to know the size of the array at object creation time, either because it is constant (e.g. defined by a #define) or because you get it with the object creation arguments.
e.g. something like that (not tested...):
class myclass: public flext_base { FLEXT_HEADER(myclass,flext_base)
public:
myclass(int size) { arr = new float[size]; AddOutInt(); FLEXT_ADDMETHOD_(0,"poke",poke); }
~myclass() { delete[] arr; }
// get an indexed sample of the array void poke(int ix) { ToOutFloat(0,arr[ix]); }
float *arr;
FLEXT_CALLBACK_I(poke) };
FLEXT_NEW_1("myclass",myclass,int)
best greetings, Thomas
Hallo, Thomas Grill hat gesagt: // Thomas Grill wrote:
Ben wrote:
I'm working on my first flext project. I'm slowly getting the hang on things (c++ in particular) I need to create an array with the number of elements specified by the creation arguments of the object. I also need this array to be available to all functions in the class.
Of course then you'll have to know the size of the array at object creation time, either because it is constant (e.g. defined by a #define) or because you get it with the object creation arguments.
With flext, because it's C++, you can also use one of the resizeable containers in the C++ STL. Depending on how you want to access them, a "vector" or a "list" might be the right container to use. Then you won't have to deal with memory management using new/delete. Just define e.g. a vector as class member. Example (untested, too):
#include <vector> class myclass: public flext_base { FLEXT_HEADER(myclass,flext_base)
public: myclass(int size) { // Init vec with zeros: while (size--) vec.push_back(0.0); AddOutInt(); FLEXT_ADDMETHOD_(0,"poke",poke); }
// use default destructor! // ~myclass() { // } // get an indexed sample of the array void poke(int ix) { ToOutFloat(0,vec[ix]); }
FLEXT_CALLBACK_I(poke) // vec could also be private: private: vector<float> vec; };
FLEXT_NEW_1("myclass",myclass,int)
This might not be necessary in your example but comes in handy for more complex uses (like defining a vector<some_synth> to hold a changing voice count of a synthesizer dynamically)
ciao