Array of subclass or superclass or something like that ...

Started by
5 comments, last by kaysik 20 years, 9 months ago
Hey, Lets say i have a class superClass, and anotherone called subClass (bet you can''t guess where this is leading!). Well to everyones supprise subClass is derived from superClass :O Now say i have an array of subClass (ie subClass myarray[10]) and I have a function process that takes an array of superClass (ie. process(superClass *array, int size);. If I cast my subClass array to superClass and pass it in it doesn''t work I''m guessing this is because subClass and superClass are differents sizes etc in memory so the pointer arithmetic gets all messed up. So how do I do it if i can''t just do: process( (superClass)myarray, 10); Any idea''s how to get round this?? thanks
Advertisement
Here's one possibility: Change the signature of the process function to accept a single superclass rather than an array, then loop through the array of subclasses passing a typecast of each element to process function.

eg.
  loop over j process( (superClass)myarray[j]); 


[edited by - lessbread on July 5, 2003 12:23:26 PM]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
It's been a while I haven't touched C/C++, but here goes:

//You should write,

...
process ( ( superClass * ) myarray, 10 );
...


I believe you are casting to an object superClass instead of an pointer by doing:

process ( (superClass) myarray, 10 );

Due to this you should be getting en error from you compiler saying: cannot convert type superClass to superClass *

[edited by - brownbean on July 5, 2003 12:49:34 PM]
I don''t know, this works just fine on GCC 3.2:
struct SuperClass{};struct SubClass : SuperClass{};void process( SuperClass *foo ){}int main( void ){  SubClass Sub[ 10 ];  process( Sub );} 
BrownBean --> I believe you are casting to an object superClass instead of an pointer by doing:

yeah my example was wrong - but my actual app is correct ... was just a bad example

Painless --> the struct thing might work ... but i''m useing classes as in:
class superClass {}
class subClass : public superClass {}
And that definatly doesn''t work for me. The first item does, say array[0].myVar comes up fine, but array[1].myVar doesn''t - which is why i think its the different memory size or something similar (also if i make it accept the subclass and change no other code then it works fine as well so it has to be the superclass/subclass thing that going wrong)

LessBread --> already thought of that ... thing is my example is way simpler than my actual code, and there are some times when the array is passed 3 or 4 functions deep which makes it almost impossible to take all the loops out. If all else fails i''ll have to do this i think ... i''m just still hoping there is another way out

thanks for the suggestions ppl
And by
quote:
I don''t know, this works just fine on GCC 3.2:

I really meant that I''m an idiot. Do what lessbread said.
quote:Original post by Painless
I really meant that I''m an idiot. Do what lessbread said.


lol - np :D

i think i''ll have to unless SOME OTHER KIND PERSON COMES ALONG???

This topic is closed to new replies.

Advertisement