Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 06-01-2005, 03:21 AM   #1
twisted-mystic
Abandonia nerd

 
Join Date: Dec 2004
Location: ,
Posts: 60
Thumbs up

:help: :help: :help:
like i said previously, i'm no C++ Guru.

say i had

class a{};

class b::a{};
class c::a{};

ok i use the class a to basically give me common ground between b and c, now, if i have a function that returns a type of class a, how do i type cast that into type b?

like this, the function has an object that is of class b(which is also part of class a), the function returns a type of class a, but i want to typecast it back to type b, this is'nt even making sense to me, LOL, hopefully someone out there will know what i'm talking about.
twisted-mystic is offline                         Send a private message to twisted-mystic
Reply With Quote
Old 06-01-2005, 03:57 AM   #2
Mucman
Newbie

 
Join Date: Dec 2004
Location: ,
Posts: 16
Default

I believe what you are asking is how inheritence and polymorphism work.

I haven't programmed C++ in a year but I can dig up an abstract example if you like.

Mucman is offline                         Send a private message to Mucman
Reply With Quote
Old 06-01-2005, 04:39 AM   #3
twisted-mystic
Abandonia nerd

 
Join Date: Dec 2004
Location: ,
Posts: 60
Default

More polymorphism
here is an Eg:

class object
{
public:
char* name;
};

class box : public object
{
public:
int width;
}
class forklift : public object

class whouse
{
void putObjectInWhouse(object)
object whatsInMyWHouse();
}


ok so the whouse object can return two things, a forklift or a box, since i want to be able to put either or both in whouse, i accept an argument of object, so i have to put out a object , object.

the problim is, when i call whatsInMyWhouse() and say i have something that can tell if it's a forklift or box, how does that function treat it like it's proper object, that is box or forklift, like this

int main()
{
whouse myWHouse;
box myBox;
myWHouse.putObjectInWhouse(myBox);
cout << myWHouse.whatsInMyWHouse().width;
}

if i do that, (given that it's coded with proper syntax), i get a compiler exception saying width is not a member of object, but it is of Box, and i know the object it's handing me is a box.... my question is, how do i treat it as a box, and not just an object..

I'm having a hard time explaining this too, so if i can claify please let me know.

And Thanks!!
twisted-mystic is offline                         Send a private message to twisted-mystic
Reply With Quote
Old 06-01-2005, 05:57 AM   #4
Rogue
10 GOSUB Abandonia
20 GOTO 10
 
Rogue's Avatar

 
Join Date: Nov 2004
Location: Afrim, Albania
Posts: 2,113
Default

I had to use them (inheritence) and it's not so hard.

Will post tomorrow more stuff about it... (it's about 2 AM, and I have to work in couple of hours )
Rogue is offline                         Send a private message to Rogue
Reply With Quote
Old 06-01-2005, 06:50 AM   #5
goblins
Game Wizzard

 
Join Date: Dec 2004
Location: ,
Posts: 250
Default

keep em coming i now have dev C++ and am going over coltoo to learn something - actually picking up a few bugs that im attempting to solve but its going to take a while to get the hang of it.
goblins is offline                         Send a private message to goblins
Reply With Quote
Old 06-01-2005, 09:42 AM   #6
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default


a function returning class a (which is in reality a class B)
then casting that "a" to a " b"

that's dynamic casting example

a* input;

b* result= dynamic_cast<b*>(input);

if b is not in reality a a then null is returned (well I think actually a exception is thrown)

you do this let a function accept both "b" and "c" through the "a" baseclass pointer.

__________________
Flowing with the stream of life
Data is offline                         Send a private message to Data
Reply With Quote
Old 06-01-2005, 02:14 PM   #7
twisted-mystic
Abandonia nerd

 
Join Date: Dec 2004
Location: ,
Posts: 60
Default

k: Thanks data, i'll have to give that a try when i get home

edit:

Still working on it, i get an error saying that my base class is'nt polymorphic, i'm working on figuring that one out now.
twisted-mystic is offline                         Send a private message to twisted-mystic
Reply With Quote
Old 11-01-2005, 05:31 AM   #8
twisted-mystic
Abandonia nerd

 
Join Date: Dec 2004
Location: ,
Posts: 60
Default

Ok i'm lost, any ideas on this "Not a polymorphic type" error?
twisted-mystic is offline                         Send a private message to twisted-mystic
Reply With Quote
Old 11-01-2005, 08:15 AM   #9
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

you need a virtual function in it.

class a {
virtual ~a();
}

then it will work.
__________________
Flowing with the stream of life
Data is offline                         Send a private message to Data
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
World Class Rugby Romano Games Discussion 14 05-04-2011 08:26 PM
Playbahnosh joins the working class Playbahnosh Blah, blah, blah... 21 06-04-2008 10:09 AM
World Class Soccer Kosta Games Discussion 6 31-07-2006 02:48 AM
Your Favorite Class Koen Gaming Zone 53 06-01-2006 07:03 PM
The Best Rpg Class Of All Time Servo Gaming Zone 1 17-03-2005 01:58 AM


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 11:50 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.