Object pointers to any type


The question is how in c++ we can have a pointer that points to anything:

In c# (not c++) object is the base for all reference types. There also is a relevant concept about boxing and unboxing.

Anyway, you can assign any instance of any type to an instance of an object class.

For example:


bool b=true;

int i=45;

object obj=i;


Despite many that say in c++ it's not possible … during my research I noticed that it looks c++ has a robust mechanism that is very reliable for it, but lack of time did not allow me to go more trough that solution.

Anyway, I have made my own simplified dummy solution for it:

C++ code

#include <iostream>

void pointerInfo(auto *objectPointer)


    std::cout << "address of object is: " << objectPointer << "\t"

              << "type of the object is: " << typeid(*objectPointer).name() << "\t"

              << "value of object is: " << (*objectPointer) << "\t"

              << "sizeof pointer is: " << (sizeof(objectPointer)) << "\t"

              << "size of object: " << (sizeof(*objectPointer)) << "\n\n";




    int i = 345;


    bool b = true;


    double d = 23.44;


    // now its time to take a dynamic pointer that can pint to anything , luckily size of (long long) is 8 and it can hold an address";

    long long address;

    std::cout << "\ndynamic object\n\n";

    address = (long long)&i;

    pointerInfo((int *)address); // in the action you have to say interpret as what (int) but if you want more dynamic without mentioning interpretation information again you have to implement however i guess there are something builtin as well in c++

    address = (long long)&b;

    pointerInfo((bool *)address);

    address = (long long)&d;

    pointerInfo((double *)address);


Also have a look at https://www.geeksforgeeks.org/void-pointer-c-cpp/

based on that, you don't even need (long long) to store an address, and it can be stored in (void* (or even any pointer (any*/int* ….)))