Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can pointers be safely and portably used as object IDs?

Tags:

c++

pointers

Suppose I have a bunch of valid pointers to instances of a class A. Can I safely and portably assume that the pointers will stay the same during the whole object lifetime? In other words, can I use a pointer to an object as a unique object ID?

like image 819
Arek' Fu Avatar asked Jun 20 '11 09:06

Arek' Fu


People also ask

Can a pointer be an object?

A pointer is a type of variable that carries location information. In this case, the example variable will store the address of an Order object that we want to interact with. We initialize the pointer variable by using the C++ new operator to construct a new object of type Order.

Can pointers be used in C#?

A pointer is simply a variable that holds the memory address of another type or variable. By default, C# does not allow you to use pointers in your apps.

What kind of objects can be associated with pointer variables?

Pointer variables may point to struct or class objects.

When would you use pointers to objects?

Pointers can be used to pass of arrays and strings to functions more efficiently. Pointers save the memory. Pointers reduce the length and complexity of a program. Pointers make possible to return more than one value from the function.


1 Answers

Sure; I don't see why not.

A memory address is pretty much the definition of object identity (taking type out of the equation for a moment).

like image 159
Lightness Races in Orbit Avatar answered Sep 18 '22 13:09

Lightness Races in Orbit