pointer to object, but object gets lost...

Hi!
So in the following code i have a class, 'Box', that saves a integer 'value'.
Then i run a function in which i set the global pointer 'my_box' on a new box(5). In that function i can still get the value of the object of the pointer, but if im outside of the function the objects gets deleted and the pointer just points to something else. so for me the output here is:

1
2
(*my_box).value = 5
(*my_box).value = -858993460


fun fact: if i comment 'DoSomethingElse();' i get 5 for both, so the object isnt deleted yet.

How can i keep my object as long as i have a pointer pointing it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Box;
class Box {
public:
	Box(int);
	int value;
};

Box::Box(int value_) {
	value = value_;
}

bool RegisterBoxes();
void DoSomethingElse();

Box* my_box;

int main()
{
	RegisterBoxes();

	DoSomethingElse();

	std::cout << "\n(*my_box).value = " << (*my_box).value; // prints something random
}

bool RegisterBoxes() {
	Box box1(5);
	my_box = &box1;

	std::cout << "\n(*my_box).value = " << (*my_box).value; // prints '5'

	return true;
}

void DoSomethingElse() {
	
}
Last edited on
1
2
3
4
5
6
bool RegisterBoxes() {
	Box box1(5);
	my_box = &box1;
	std::cout << "\n(*my_box).value = " << (*my_box).value;
return true;
}


You are trying to assign the address of a local variable box1 to my_box. When a function ends (RegisterBoxes()), it always tends to destroy all of its local variables, that's why when you try to output my_box again you only see garbage (because it points to a piece of data that has already been destroyed)

To solve this, use dynamic allocation (also remember to free it when nolonger necessary) :
1
2
3
Box box1(5);
my_box = new Box(0);
*my_box = box1; 
Last edited on
Thank you!
So i always have to create an empty constructer Box::Box() {} as well?
Well, every simple or non-complex struct should have that (if they have a constructor other than the default constructor). Just initialize all neccessary members of a struct with default values.
No need for another constructor here, instead of
1
2
3
    Box box1(5);
    my_box = new Box(0);
    *my_box = box1; 

just put
 
    my_box = new Box(5);


Also when you use new, remember to have a corresponding delete. Just before the end of main(), you should put,
 
    delete my_box;
So i always have to create an empty constructor Box::Box() {} as well?
Not necessarily. If you do not supply any constructors, the compiler will provide a default constructor for you. However, you provided a constructor at line 4 that requires an int, so the compiler will not automatically provide a default constructor.

Since it's a good idea to initialize value, you can avoid having two constructors by providing a default value in your constructor.
1
2
3
4
5
//  At line 4: 
  Box (int val = 0);  // value will be initialized to 0 if not provided
...
  Box a (5);  //  Initializes value to 5 
  Box b;      //  Initializes value to 0 

Last edited on
Topic archived. No new replies allowed.