By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Making lokal defined Multidimensional Array Global

P: n/a
Hi

I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For
this i took a template script in order to create the array in the funktion.
The Size of the Array I have to calculate first before I create it.So I have
to init it inside my Funktion. Now how do I get this Array so that it is
globally accessible.

Thanx
Patrick
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Patrick" <th**********@spamgourmet.com> wrote in message
news:ck**********@home.itg.ti.com...
Hi

I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For
this i took a template script in order to create the array in the funktion. The Size of the Array I have to calculate first before I create it.So I have to init it inside my Funktion. Now how do I get this Array so that it is
globally accessible.

Thanx
Patrick


Impossible, arrays cannot be copied in C++. Either create the array outside
the function and pass a pointer to it inside the function. Or, better still,
switch to using vectors instead of arrays, unlike arrays vectors can be
copied, so you can just write

#include <vector>
using namespace std;

vector<vector<int> > create2dVector()
{
vector<vector<int> > vec;
// init the vector here
...
return vec;
}

john
Jul 22 '05 #2

P: n/a

"Patrick" <th**********@spamgourmet.com> wrote in message
news:ck**********@home.itg.ti.com...
Hi

I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For
this i took a template script in order to create the array in the
funktion.
The Size of the Array I have to calculate first before I create it.So I
have
to init it inside my Funktion. Now how do I get this Array so that it is
globally accessible.

Thanx
Patrick


Globals are evil. :-) I'd reconsider that idea, if possible. Do you really
need it accessible by all classes and functions? Perhaps you might consider
having a class that contains both the array object, and any other objects
and code that need access to the array.

Many new programmers start out by writing their programs as one big main()
function, which calls other functions, in a precedural fashion. Using
object-oriented approaches often leads to better code, for many reasons.
Instead, you might want to declare a local instance of this new "container"
class in your main() function, and let it do all the work you now have in
main(). (Assuming that's the way you've structured it, of course...you
haven't shown any code, so I'm guessing.)

At the very least, I'd put the array into a class. Then you can use it
however you please. You can create it in one place, and pass a pointer to
it to other functions that need to work on it. Or you can declare it
global, but wait until your program is ready to "initialize" it, calling a
member function to do the actual creating of the internally stored array.

In any case, wrapping it in a class would be my first task.

-Howard
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.