|
Memberlist | Forum Rules | Search | Today's Posts | Mark Forums Read |
Search Forums: | Click here to use Advanced Search |
![]() |
|
Thread Tools | Display Modes |
![]() |
#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. |
||
![]() ![]() |
|
![]() |
#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. |
||
![]() ![]() |
|
![]() |
#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. |
||
![]() ![]() |
|
![]() |
#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 |
||
![]() ![]() |
|
![]() |
#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.
|
||
![]() ![]() |
|
![]() |
#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 |
||
![]() ![]() |
|
![]() |
#7 | ||
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2004
Location: ,
Posts: 60
|
![]() 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. |
||
![]() ![]() |
|
![]() |
#8 | ||
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2004
Location: ,
Posts: 60
|
![]() Ok i'm lost, any ideas on this "Not a polymorphic type" error?
|
||
![]() ![]() |
|
![]() |
#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 |
||
![]() ![]() |
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
World Class Rugby | Romano | Games Discussion | 14 | 05-04-2011 09:26 PM |
Playbahnosh joins the working class | Playbahnosh | Blah, blah, blah... | 21 | 06-04-2008 11:09 AM |
World Class Soccer | Kosta | Games Discussion | 6 | 31-07-2006 03:48 AM |
Your Favorite Class | Koen | Gaming Zone | 53 | 06-01-2006 08:03 PM |
The Best Rpg Class Of All Time | Servo | Gaming Zone | 1 | 17-03-2005 02:58 AM |
Thread Tools | |
Display Modes | |
|
|
||
  |