5

I am trying to create a class, that contains a std::vector of void*. I have been told, that void* is the C++ equivalent to Object in Java. Since this is the C++ port of a program written in Java it should work in theory.

Java:

ArrayList<Object> list;

C++:

vector<void*> list;

This won't compile, giving the error: "'reference': illegal use of type 'void'".

Is void* really the C++ equivalent of Java's Object? Am i using it the wrong way?

Mephy
  • 2,978
  • 3
  • 25
  • 31
l'arbre
  • 719
  • 2
  • 10
  • 29
  • 1
    void * in C++ is most definitely not equivalent to Object in java. It is a data type that holds a generic pointer to a memory location. – JJF Oct 25 '15 at 18:16
  • The vector declaration, on its own, should compile but you didn't show the full code. You're probably doing something you shouldn't with the vector. – aslg Oct 25 '15 at 18:18
  • 3
    Good code rarely needs to use `void*`. There is almost always a better way to design it. Use a vector of base class smart pointers. – Neil Kirk Oct 25 '15 at 18:31
  • 1
    Whoever told you that `void*` that has an interesting unerstanding of C++. For most cases I would say that there is design issue when you use ´void*´, and probably the same goes for your `ArrayList`. What do you do with it without casting it? – Jens Oct 25 '15 at 22:10

4 Answers4

6

A Java Object is a fundamental base-class that provides some common properties to all Java classes.

There is no such thing in C++. If you want to design a polymorphic hierarchy, you design your own base-class MyBaseClass (abstract or not), and then design derived classes.

Thus, it's technically feasible to create a vector<void*> container, but it is meaningless. For proper software design, what you want is to design your base-class MyBaseClass, so that you will be able to create vector<MyBaseClass*> containers.

Daniel Strul
  • 1,458
  • 8
  • 16
3

A pointer to a void means that you have the memory address of something but you don't know what type that something is. You can create your vector with any kind of pointer, then when you add items to the vector you can cast your pointers to whatever pointer you used in the vector declaration. It's hokey but it will work syntactically. The down side is that you won't know what data items are in the vector.

nicomp
  • 4,344
  • 4
  • 27
  • 60
1

You're right on both points.

  1. void* is NOT the C++ equivalent of Object.
    There is no equivalent in C++.
  2. You're using it in a wrong way – you're dereferencing an element in the vector.

It's impossible to provide any advice about what you should do, as you have not provided any information about what you want to accomplish.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you! Just so you know, your answer helped, but Delmer gave me a solution to my problem, so I accepted his one. Thanks alot though! – l'arbre Oct 25 '15 at 18:27
1
  1. void* vs Object. These are certainly not equivalents, void* variables contain addresses to memory without a type (it has no methods and can also point to primitives), an Object variable is a reference to object which is an instance of the Object class or one of it's sub-classes (and so has some methods, the possibility of polymorphism, and cannot reference a primitive type).

    What is probably meant by the comparison is that if in c++ you want to store objects of any class you would us a void* and then cast as needed when evaluating. In this way they can do some of the same things.

  2. The compile error is probably caused by what you're doing with list after it's created, since as explained void* can not be treated as Object would.

Community
  • 1
  • 1
Linus
  • 894
  • 7
  • 13