Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kaysik

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

This topic is 5555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 );
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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???

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!