Making lokal defined Multidimensional Array Global

P

Patrick

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
 
J

John Harrison

Patrick said:
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
 
H

Howard

Patrick said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top