type checking

T

Tim Frink

Hi,

in a book I've found this argumentation for templates instead of void*:
"When using void pointers, the compiler cannot distinguish types, so it
cannot perform type checking or type-specific behavior such as using
type-specific operators, operator overloading, or constructors and
destructors."

What is exactly meant by "type checking" and why is this so important?
Could you provide an example where this might be exploited (and where
void* would fail).

I want to implement a std::list< void* >, so would it be beneficial to
use templates instead? In my case, I see no use for type-specific
operators and operator overloading. Or do I forget something?

Thank you.

Regards,
Tim
 
I

Ian Collins

Tim said:
Hi,

in a book I've found this argumentation for templates instead of void*:
"When using void pointers, the compiler cannot distinguish types, so it
cannot perform type checking or type-specific behavior such as using
type-specific operators, operator overloading, or constructors and
destructors."

What is exactly meant by "type checking" and why is this so important?
Could you provide an example where this might be exploited (and where
void* would fail).
Putting an Apple* in a list of Orange*?
I want to implement a std::list< void* >, so would it be beneficial to
use templates instead? In my case, I see no use for type-specific
operators and operator overloading. Or do I forget something?
What do you intend to put in your list and what are you going to do with it?
 
T

Tim Frink

Putting an Apple* in a list of Orange*?

I don't understand this. When I have a list< void* >, so it's unknown
that I have a list of Orange*. Thus, it can't be said that I put wrong
data types into the list.
What do you intend to put in your list and what are you going to do with
it?

The list is in class A and should collect pointer to objects of class B.
However, class A should be free of any class B members, so list<class B*>
is not allowed. This list will than be read by another class C and the
list elements will be there dynamic_cast'ed back into class B.
 
I

Ian Collins

Tim said:
I don't understand this. When I have a list< void* >, so it's unknown
that I have a list of Orange*. Thus, it can't be said that I put wrong
data types into the list.
Some piece of code may assume the list contains Orange* (there is no way
of checking with void*) and another might insert an Apple*, there isn't
any type checking.
The list is in class A and should collect pointer to objects of class B.
However, class A should be free of any class B members, so list<class B*>
is not allowed. This list will than be read by another class C and the
list elements will be there dynamic_cast'ed back into class B.

You can't dynamic_cast from void*.

If the list contains B*, it should be a list of B*. There aren't any
"class B members" involved.
 
T

Tim Frink

The list is in class A and should collect pointer to objects of class B.
You can't dynamic_cast from void*.

Would a static_cast work?
If the list contains B*, it should be a list of B*. There aren't any
"class B members" involved.

Why not? :)
When I have the list< classB* > in my class A, so class A must know what
classB is. Either by including the headers of class B or by a class
forward declaration. And this is what I want to avoid.
 
I

Ian Collins

Tim said:
Would a static_cast work?
Yes.


Why not? :)
When I have the list< classB* > in my class A, so class A must know what
classB is. Either by including the headers of class B or by a class
forward declaration. And this is what I want to avoid.

All a forward declaration does is tell the compiler there is a class B.
That's all. It does not need to know anything about class B.

Any consumer of the list would have to knows what class B was to create
or use one.

A don't see a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top