06-01-2005, 03:21 AM | #1 | ||
Join Date: Dec 2004
Location: ,
Posts: 60
|
: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. |
||
|
|
06-01-2005, 03:57 AM | #2 | ||
Join Date: Dec 2004
Location: ,
Posts: 16
|
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. |
||
|
|
06-01-2005, 04:39 AM | #3 | ||
Join Date: Dec 2004
Location: ,
Posts: 60
|
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!! |
||
|
|
06-01-2005, 05:57 AM | #4 | ||
Join Date: Nov 2004
Location: Afrim, Albania
Posts: 2,113
|
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 ) |
||
|
|
06-01-2005, 06:50 AM | #5 | ||
Join Date: Dec 2004
Location: ,
Posts: 250
|
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.
|
||
|
|
06-01-2005, 09:42 AM | #6 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
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 |
||
|
|
06-01-2005, 02:14 PM | #7 | ||
Join Date: Dec 2004
Location: ,
Posts: 60
|
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. |
||
|
|
11-01-2005, 05:31 AM | #8 | ||
Join Date: Dec 2004
Location: ,
Posts: 60
|
Ok i'm lost, any ideas on this "Not a polymorphic type" error?
|
||
|
|
11-01-2005, 08:15 AM | #9 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
you need a virtual function in it.
class a { virtual ~a(); } then it will work.
__________________
Flowing with the stream of life |
||
|
|
|
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 |
|
|
||
  |