Advertisement

Object Usage and Destructors

Started by June 05, 2017 01:36 PM
24 comments, last by Josheir 7 years, 5 months ago
Not exactly. It doesn't make sense to say "you should have used new", because (as above) using new makes absolutely no difference to whether a destructor is called. The destructor is called when the object is destroyed, no matter how it gets destroyed, or how it got created.

Well what I was saying is if there is some different type object in an object and it is pointed to it better not be a regular pointer or we will never be able to destroy any of it's different typed objects that may need deleting.

delete causes a deconstructor.

(tired,)

josheir

"In an object that has a different object, new can be used to create a pointer to that object as shown."

If object A already 'has' object B, then you wouldn't be calling new. New creates an object, and returns a pointer to it.

Object A might contain a data member that is a pointer, something like B* objB, which on its own doesn't point to anything. You need to assign a valid pointer to that for it to be usable. Usually that means creating an instance of Class B via 'new', and assigning that value to the variable called 'objB'. Later, you'd want to call delete on that pointer, so that the object that was created with new gets destroyed properly.

There are ways to create pointers other than via new, ways which don't actually create a new object, but that's out of the scope of this discussion.


Well what I was saying is if there is an some different type object in an object and it is pointed to it better not be a regular pointer or we will never be able to destroy any of it's different typed objects that may need deleting.

The fact that the types involved may be different is irrelevant.

Regular pointers are okay. You just have to remember to call delete on them at the appropriate time. Destructors exist for this reason, giving you an opportunity to do any clean-up code when an object is going out of scope - such as deleting any sub-objects that it owns and has pointers to.

Smart pointers are just wrappers for normal pointers, but they have their own destructor that calls delete on the object they point to, meaning you don't have to remember to do it.

Advertisement

I think I understand what you're asking.

In an object that has a different object,

From your example code, class B contains a pointer to an object of type A: A * testObject2;

new can be used to create a pointer to that object as shown. What about creating a pointer to that object that is a data member instead?

In that code the data member testObject2 is a pointer, but initially it doesn't point to anything in particular. You should not use it yet because you have no idea what it points to, and consequently it will probably crash your program or worse. You need to actually point the pointer to something. Usually you point things to null.

Since it is a pointer, it can be made to point to anything of the right type. Since it is a pointer to A, it can point to any object instance of class A. It can also point to a special value meaning "this does not point to anything", called NULL, nullptr, or 0.

You can point it to anything of the right type that makes sense for your program. You might set it to point to testObject1 since it is an instance of class A. You could set it to point to testObjA or test_1 since those are also instances of class A. You could create an object with new, and point to the newly created object.

You can even make data members that point to the object itself. In fact, one of these is automatically created for you, called "this".

There is an important detail for whatever pointers you use. Pointers point to things. If you point to something and then the thing gets destroyed, the pointer will continue to point to that spot. Even though the pointer still points there, once the object is destroyed the pointer is pointing to dead memory. If you try to use the pointer after something is destroyed your program will misbehave, and probably crash.

Which brings us to:

Is this good or bad for what reasons?

It is not good or bad. What really matters is that pointers point to things that actually exist or they be changed to point to a null value (NULL, nullptr, or 0). Controlling the lifetime of objects is an important part of programming and one of the most common reasons for programs to crash. Smart pointers can help with this, but ultimately the decision is up to the programmer to do whatever works for the program they're building.

Regular pointers are okay. You just have to remember to call delete on them at the appropriate time.

Not dynamically allocated pointers with new, regular pointers:

class A{

}

class B{

//'regular' maybe its called raw instead?

A * object;

}

I do enjoy the terminology, sincerely,

Josheir

Not dynamically allocated pointers with new, regular pointers:

Pointers can point to anything of the right type. You can point it to an existing object of that type, you can create an object dynamically with new and have it point to the new instance (and eventually call delete on that object), you can point it to NULL or nullptr or 0 to say it doesn't point at any object.

Thank you Kylotan and frob,

Josheir

This topic is closed to new replies.

Advertisement