nacho4d
nacho4d

Reputation: 45128

How to know the type of an object in a list?

I want to know the type of object (or type) I have in my list so I wrote this:

void **list; //list of references
list = new void * [2];
Foo foo = Foo();
const char *not_table [] = {"tf", "ft", 0 };

list[0] = &foo;
list[1] = not_table;

if (dynamic_cast<LogicProcessor*>(list[0])) { //ERROR here ;(
    printf("Foo was found\n");
}
if (dynamic_cast<char*> (list[0])) { //ERROR here ;(
printf("char was found\n");
}

but I get :

error: cannot dynamic_cast '* list' (of type 'void*') to type 'class Foo*' (source is not a pointer to class)
error: cannot dynamic_cast '* list' (of type 'void*') to type 'char*' (target is not pointer or reference to class)

Why is this? what I am doing wrong here? Is dynamic_cast what I should use here?

Thanks in advance

EDIT:

I know above code is much like plain C and surely sucks from the C++ point of view but is just I have the following situation and I was trying something before really implementing it:

I have two arrays of length n but both arrays will never have an object at the same index. Hence, or I have array1[i]!=NULL or array2[i]!=NULL. This is obviously a waste of memory so I thought everything would be solved if I could have both kind of objects in a single array of length n.

I am looking something like Cocoa's (Objective-C) NSArray where you don't care about the type of the object to be put in. Not knowing the type of the object is not a problem since you can use other method to get the class of a certain later. Is there something like it in c++ (preferably not third party C++ libraries) ?

Thanks in advance ;)

Upvotes: 0

Views: 488

Answers (4)

Puppy
Puppy

Reputation: 146998

You shall use a boost::variant or boost::any to accomplish this purpose. dynamic_cast only works when the source and target types are linked by inheritance. Also, using void*s is terrible, terrible style in the overwhelming majority of code, as it's completely unsafe.

Actually, reading your code, I just suggest that you get a C++ basics book.

EDIT: dynamic_cast only works on types which are linked by inheritance AND have at least one virtual function in the base class.

Upvotes: 2

j_kubik
j_kubik

Reputation: 6181

Your code is terrible for C++, however in C those things are quite common... if you don't use any c++ features in your program, perhaps you should change question category to C?

If you want to make it C-like then i would suggest something like that

enum type{ type1, type2, typeChar, typeFoo }

struct ptr{
  void * p;
  type t;
};

ptr* list=new ptr[2];
list[0].p = &foo;
list[0].t = typeFoo;
list[1].p = not_table;
list[1].t = typeChar;

if (list[0].t == typeChar){
  printf("char was found\n");
} 
if (list[0].t == typeFoo){
  printf("Foo was found\n");
}

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361692

Before you do anything of that sort, I would suggest you to read this:

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

Read at least first two replies.

Upvotes: 1

Jeremy Friesner
Jeremy Friesner

Reputation: 73294

Dynamic_cast doesn't work on void pointers. You'll need to either store typed pointers in your list instead (e.g. Pointers to a common base class) or use reinterpret_cast instead (and be very careful with it since reinterpret_cast will succeed even if the conversion doesn't make any sense!)

Upvotes: 1

Related Questions