View Full Version : C++ Custom Class Typecasting
twisted-mystic
06-01-2005, 03:21 AM
: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.
Mucman
06-01-2005, 03:57 AM
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.
twisted-mystic
06-01-2005, 04:39 AM
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.
:cheers: And Thanks!!
Rogue
06-01-2005, 05:57 AM
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 :()
goblins
06-01-2005, 06:50 AM
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.
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.
twisted-mystic
06-01-2005, 02:14 PM
:ok: 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. :cheers:
twisted-mystic
11-01-2005, 05:31 AM
Ok i'm lost, any ideas on this "Not a polymorphic type" error?
you need a virtual function in it.
class a {
virtual ~a();
}
then it will work.
vBulletin® v3.7.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.