![]() |
Blitz Support : |
From: Julian Cummings (cummings_at_[hidden])
Date: 2003-11-11 17:08:08
Hello Ben,
It seems like you are looking to construct an Array in main() but
actually initialize
the Array somewhere else. That is perfectly fine as long as the
initializing function
can refer to the Array object somehow. The problem with your code below
is that
the blitz Array object test2D initialized in Data::initializeArrays() is
not the same
as the object myArray back in main().
There are several ways of dealing with this, depending on who should own
the Array.
If you want to define the Array as a global static variable before the
main() function,
as you did below, then you would need an extern declaration of the same
Array that
was visible in the Data class definition. That way, Data could refer to
the Array object
in order to initialize it. Generally speaking, such global static
objects are deprecated
in C++, since they violate the concept of encapsulation. If you declare
an Array object
within the main() function, then it really makes sense to pass the Array
to the Data class
in order to have it be initialized. You could do this in the Data
constructor or in the
call to Data::initializeArrays(). A third way to go would be to have
the Data class actually
own the Array as a data member. Then the Array would get constructed
when the Data
class was created.
Another important thing you should be aware of is the resize() method of
the blitz Array.
If you construct a blitz Array with no constructor arguments, you get an
Array with the
default storage mechanism (C-style array storage) and zero size. You
can then call the
resize() method on this Array to set the extents of the array, using
either a set of ints, a set of blitz Range objects, or a blitz
TinyVector of ints. This is the approach to use when you do
not know at Array construction time what size of Array you want. (You
do have to set the
element type and array dimension at compile time, of course.) Given
this, you might change
your example code as follows:
// main()
using namespace std;
#include "Data.h"
#include "blitz/Array.h"
Data *myData;
Array<double,2> myArray;
int main(inyt argc, char *argv[]) {
// construct Data object pointed to by myData somehow ...
myData->initializeArrays();
cout << "Array myArray = " << myArray << endl;
return 0;
}
// Data.cpp
#include "Data.h"
#include "blitz/array.h"
extern Array<double,2> myArray;
int Data::initializeArrays() {
int nx = ny = 3;
myArray.resize(nx,ny);
myArray = 0.0;
return 0;
}
I hope this example clears things up for you. By the way, the issue of
the blitz namespace has nothing to do with your current problem. When
you say "using namespace blitz", this
merely brings all the types and methods defined within namespace blitz
into the current
scope. It does not create any Array objects or anything like that.
Regards, Julian C.
Ben McLean (finlaylabs) wrote:
> Hi all,
>
> after downloading basic cygwin and adding in gcc, make, etc, I have
> been able to (error-free) make Blitz. Very impressive! A bit of
> jiggling in Dev-C++ and it is a useable tool already. Well done and
> thanks.
>
> All the docs and examples use
> using namespace blitz;
>
> As I am (trying to) use blitz as part of a larger multi-class project
> i do not want to use namespace blitz, rather i would prefer to use a
> constructor to create instances of Array or whatever that I can use in
> main or from any other class, especially if i actually create arrays
> in a class functiuon outside of main. What is the syntax for doing
> this? My best guesses do not work, an example guess that may clarify
> what i am trying to do is below. I would appreciate edits / comments
> on the basic example below from anyone who can show me how to do this.
>
> Thanks, Ben.
> ____________________________________________
> // main()
> using namespace std;
>
> #include "Data.h"
> #include "blitz/array.h"
>
> Data *myData;
> Array myArray; //or use Blitz::Array myArray; or blitz myBlitz;,
> something... need help here
>
> int main(int argc, char *argv[]){
>
> Data::initializeArrays();
>
> cout << "Array test2D = " << myArray.test2D << endl;//access an
> array created
>
> // within another class
>
> return(0);
> }
> _____________________________________________
> // Data.cpp
> #include "Data.h"
> #include "blitz/array.h"
>
> int initializeArrays(){
> int nx=ny = 3;
> blitz::Array<double,2> test2D(nx,ny);//made the array here,
> // but want to access it from main() or elsewhere
> test2D = 0.0;
> return(0);
> }
> _____________________________________________
> // Data.h
> #include "blitz/array.h"
>
> class Data{
> public:
> Data(Params *myParams);//constructor
> ~Data();//destructor
>
> int initializeArrays();
> };
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Blitz-support mailing list
>Blitz-support_at_[hidden]
>http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>
>
-- Dr. Julian C. Cummings E-mail: cummings_at_[hidden] California Institute of Technology Phone: 626-395-2543 1200 E. California Blvd., Mail Code 158-79 Fax: 626-584-5917 Pasadena, CA 91125