I want to do something like this:
SomeType *y;
/* ... snip ... */
auto x = new decltype(y); // Create a new pointer "x" to a SomeType object.
But decltype(y)
is SomeType*
and decltype(*y)
is SomeType&
. Is there a way to get plain SomeType
out of y
?
There are majorly four types of pointers, they are: Null Pointer. Void Pointer. Wild Pointer.
In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type.
Every pointer is of some specific type. There's a special generic pointer type void* that can point to any object type, but you have to convert a void* to some specific pointer type before you can dereference it.
There are several reasons: Not all addresses are created equal; in particular, in non Von Neuman (e.g. Harvard) architectures pointers to code memory (where you often store constants) and a pointers to data memory are different.
Since decltype(*y)
is a reference, you can use std::remove_reference
:
new std::remove_reference<decltype(*y)>::type;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With