Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / an array of classes?

Author
Message
shiny
20
Years of Service
User Offline
Joined: 17th Jan 2004
Location: Santa Monica
Posted: 12th May 2005 16:35
Heya!

I'm making a grid based game. I want to make a 2d array of classes which represents the playing bord. The only problem is, I want each block in the array to be able to point to a different class. Is there a generic type of pointer that I can use to point to different classes? Might I be going about this the wrong way?

Thanks!

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th May 2005 21:44
Quote: "Is there a generic type of pointer that I can use to point to different classes?"


Dodgy - basically, you need to know the pointer type when you extract it from your array to be able to use it.

What you probably need is to design a virtual interface type, and the declare concrete types based on that. Then your pointers will be pointers-to-interface types.



This code will print 10 and 20, proving that each concrete type is different.

Your array code might look something like this:



Naturally, I'd use a smart pointer (probably boosts shared_ptr class) so that I wouldn't get leaks either ...

*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins and source code http://www.matrix1.demon.co.uk
shiny
20
Years of Service
User Offline
Joined: 17th Jan 2004
Location: Santa Monica
Posted: 13th May 2005 03:55
Oh my! You have been so helpfull!

Thankyou very much!

Its amazing how big hurdles get sometimes! Thanks for helping me leap over it!

: ).

I'm wondering, might this be considered bad programing technique though? The problem being that I'd need to declare void functions in the base class for every function I derive from it.... and I'm going to have a hell of allot of functions!

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th May 2005 05:48
Well, one alternative is to use a tagged pointer. That's basically a structure that holds a void pointer alongside an integer that indicates the pointers type. You would use this 'type' to cast your void pointer to the correct pointer type.

It is very definitely *not* a good way to do it. You make your code very unsafe by doing it, as you only need to get the pointer cast slightly wrong to corrupt memory or even crash your program.

If you can do it using an interface class then I really suggest that you do it that way.

*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins and source code http://www.matrix1.demon.co.uk
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 13th May 2005 21:32 Edited at: 13th May 2005 21:36
Also, if you need to know what type of child your pointer's pointing at,
you should add an "type-identifying" method for when you don't want to do downcasting's with dynamic_cast<> (works only with polymorphic classes).

//Pentium IV 3200E/Prescott; 800Mhz FSB; Hyperthreading; WinXP Pro (!XP2); ATI R9700PRO; 1024MB RAM (2x 512MB "DualChanneled"; VC++ 6.0; Delphi; ADSL;
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 13th May 2005 21:34 Edited at: 13th May 2005 21:35
Quote: "Naturally, I'd use a smart pointer (probably boosts shared_ptr class) so that I wouldn't get leaks either ...
"


A very good suggestion from IanM here!!!

//Pentium IV 3200E/Prescott; 800Mhz FSB; Hyperthreading; WinXP Pro (!XP2); ATI R9700PRO; 1024MB RAM (2x 512MB "DualChanneled"; VC++ 6.0; Delphi; ADSL;

Login to post a reply

Server time is: 2024-04-25 15:55:25
Your offset time is: 2024-04-25 15:55:25