There is no equivalent to this in C ++, and it would be pointless to try to program Java in C ++. At the same time, I approach this in terms of attempts to imitate as much as possible the characterization and spirit of the assignment. Each method that I propose has disadvantages and limitations. The first two are not truly idiomatic C ++, but it is important to know about them in order to see what problems the last two solved.
1. C-style void pointers.
Let me start with the most basic and least useful void pointer:
void* foo = new Foo();
, void , .. void. : . -, ++ - . , . , , .
:
void* foo = some_function( _arg0 );
, , , . , , , , , , , , .
2. C-style Unions
N , , java.lang.Object, . , POD. : , , -POD-. , std::string.
, :
union myType{
int a;
char b[4];
};
char "b" "myType", int . ++ (, ..). ++.
3. Boost:: Any
, " ", Boost:: Any. , , . Boost , , . Any:
, ( , ) : , , , ++.
, void, .
4. ::
Boost:: Variant , . , -POD. , :
, ( Hen01 , , *). (, dynamic_cast, boost:: any_cast ..).
- :
- Downcast . , downcast- .
- . , , . , , , .
Edit:
, , , . .