Global Class?

I'm wondering if there is a way to declare an object in a function, and have it accessed in other parts of the program. I have the class and constructor defined at the beginning of the program. main() makes a call to another function, and inside that function several instances of an object are created. I need the data stored in those objects accessible in other functions though.

If you have any suggestions or other ways to accomplish this I would appreciate it. If you need anymore information just let me know.

P.S. Sorry if I sound like an idiot, I'm still a beginner.
store em in a vector.
This is more a question of ownership.

One class/function should be the clear owner of this object (or group of objects). If the object is needed in other classes/functions, then the owner should pass it to those functions as needed.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
void func1(MyClass& a) { /*...*/ } // some functions that use a MyClass
void func2(MyClass& a) { /*...*/ }

int main()
{
  // here, main() is the owner of this object:
  MyClass obj;

  // but we want other functions to use it:
  func1(obj);  // so we pass it to them

  func2(obj);
}


The owner should be the one at the "top" of the access pyramid. When the owner is finished using the object, everyone is finished using the object. Here, main is the clear owner.

To make it easier... generally the owner should be the one creating and maintaining the object. I'm doing that in the above example -- main() is the owner of 'obj', so main() is creating it.


What it sounds like you're doing is you're creating it in another function, then you want to pass ownership to someone else. I try to avoid passing ownership as it leads to very confusing code, bugs, and other problems -- but all of that can be minimized if done properly.

In your case, I would have a function return the object from it, but keep main as the owner:

1
2
3
4
5
6
7
8
9
10
11
12
MyClass MakeAnObject()
{
  MyClass stuff;
  stuff.DoThings();  // make some stuff

  return stuff;  // and return it
}

int main()
{
  MyClass obj = MakeAnObject();  // get the object from MakeAnObject
}



Here, main() is still the owner, even though you arguably created the object in MakeAnObject.


For Groups of objects, it the same idea. Someone must be the clear owner of the group. As rocketboy suggested, this can be easily accomplished by using std::vector<MyClass>.
Last edited on
Can someone please explain how to use vectors please? I'm not really sure what they are or how they work.
They're like arrays, but easier:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

int main()
{
  // if we want 5 ints:
  std::vector<int> vec(5);

  // then we can access each one with the [] operator:
  vec[0] = 4;
  vec[1] = 3;
    // etc

  // resize so that we have 15 ints:
  vec.resize(15);

  // add one int to the end:
  vec.push_back(6);  // size is now 16

  // how many ints do we have?
  std::cout << vec.size();  // prints "16"
}
Topic archived. No new replies allowed.