# Untitled

## Recommended Posts

Log059    122
im having a bit of trouble I have a class called line and a class called arc that are not derived from eachother I am trying to have an array containing both data types. To try this idea out I tried: void* entity; entity=new LINE(); entity->midpoint();***** from this line I get: C:\Documents and Settings\Jay\Desktop\lines.cpp(84) : error C2227: left of '->midpoint' must point to class/struct/union Can anyone shed some light on how I would achieve this? I dont think an abstact base class or derived classes would work since both classes have drastically different fuctions. Thanks in advance for your help

##### Share on other sites
nmi    978
You can do something like this:
struct Arc { ... };struct Line { ... };typedef union {  Arc arc;  Line line;} arc_line;arc_line al[10];al[0].arc.arcMethod(...);al[1].line.lineMethod(...);

##### Share on other sites
Guest Anonymous Poster
ummm... i dont think you do void pointers...

##### Share on other sites
Monder    993
Well you can hack your way round the problem (e.g. using what nmi did) but if they're not derived from a base class and have dratically different functions they probably don't belong in the same array anyway.

However line and arc seem to be pretty similar things, why can't they both be derived from a shape or a shape2d or some other similarly named class?

##### Share on other sites
Bincho    194
The issue stems from the fact that your pointer is void. This pointer points to some data, but the compiler doesn't know what function you want to call. Sure, you've named it but how many classes might have that same name? You need to cast the pointer to the correct type before you use it.
void* entity;entity=new LINE();reinterpret_cast<LINE>(entity)->midpoint()

That should work, but I think the problem lies with your design. You cannot hold two different classes in the same array without hacking a solution or using polymorphism. To do what I've posted you must know, ahead of time, what type each void pointer is pointing to. This is tricky unless you hide them in a pair with an enumeration (integer) and then put that pair in an array (or a vector, which would be the better choice)

Anyway, good luck.

##### Share on other sites
Log059    122
Say I derive line and arc from the same base class called entity with midpoint as a virtual function

line has the mamber fuction midpoint while arc does not use this fuction. In my ARC class I do not create a midpoint function. What can I do to avoid an error besides create a 'dummy' midpont fuction in ARC

entity[0]=new LINE();
entity[1]=new ARC();*****gives error below

C:\Documents and Settings\Jay\Desktop\lines.cpp(82) : error C2259: 'ARC' : cannot instantiate abstract class due to following members:
C:\Documents and Settings\Jay\Desktop\lines.cpp(64) : see declaration of 'ARC'

##### Share on other sites
Mattman    336
Quote:
 Original post by Anonymous Posterummm... i dont think you do void pointers...

Try it for yourself :)