Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast/convert pointer to reference in C++

People also ask

Can you cast a pointer in C?

In the C language, casting is a construct to view a data object temporarily as another data type. When you cast pointers, especially for non-data object pointers, consider the following characteristics and constraints: You can cast a pointer to another pointer of the same IBM® i pointer type.

Can we reference a pointer?

References to pointers can be declared in much the same way as references to objects. A reference to a pointer is a modifiable value that's used like a normal pointer.

Can pointer be Typecasted?

Pointer is merely a memory address. With typecasting, any type with enough size to hold the memory address can work like a pointer.

How do I get a reference pointer?

Once a reference is established to a variable, you cannot change the reference to reference another variable. To get the value pointed to by a pointer, you need to use the dereferencing operator * (e.g., if pNumber is a int pointer, *pNumber returns the value pointed to by pNumber .


Call it like this:

foo(*ob);

Note that there is no casting going on here, as suggested in your question title. All we have done is de-referenced the pointer to the object which we then pass to the function.


foo(*ob);

You don't need to cast it because it's the same Object type, you just need to dereference it.