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 / Classes question

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 28th Oct 2008 18:01
Is it possible for class 1 to inherit class 2 but class 2 to be able to access some of the objects in class 1.

Here is a simple example:
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 28th Oct 2008 18:50 Edited at: 28th Oct 2008 18:51
I don't know why you'd want to do this, but here:



[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 28th Oct 2008 20:10
I'm interesting to know why you'd need to do this. Usually you don't, which suggests you may be going the wrong way about something.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 28th Oct 2008 20:35 Edited at: 28th Oct 2008 20:36
In short, I have several classes and some of these use inheritance. The base classes allocate memory based on a variable stored in a derived class. There are also some functions in the base classes that require a variable in the derived class.

What I'm doing at the moment is storing a pointer to the derived class inside the base class. Construction code is in the function 'Construct' so that the pointer can be passed to the base class.

Here is an example:
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 28th Oct 2008 21:48 Edited at: 28th Oct 2008 21:49
Why not just have the 'set' function in the derived class then?

That function will only work for one derived class, so you may as well move it to the derived class.

[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 29th Oct 2008 21:44
Yes, it does appear that you are trying to create your class hierarchy upside-down.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 30th Oct 2008 02:29
That example is a little too simplistic. In the below example the two derived classes and the base class need access to the instance class. Due to the fact that there are two derived classes, if the code that required use of the instance class were to be moved from the base class into the derived classes then there would be repeated code. Note that this example is a simple one, obviously the derived classes should not be doing exactly the same thing.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 30th Oct 2008 12:24 Edited at: 30th Oct 2008 12:25
I'm still not sure WHY you'd want to do this, but you can replace the Construct function in BaseClass with an actual constructor.
The code for the derived class constructors would then look like this:



[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 9th Nov 2008 17:16
A little late on this subject since I've been out of town for the last couple of weeks. I really don't have time to read all of this, but I'd like to suggest that the access level protected might be useful.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Login to post a reply

Server time is: 2024-09-30 11:18:44
Your offset time is: 2024-09-30 11:18:44