Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

evilchicken

functions and inheritence

This topic is 5824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how come I cant do this
    
class foo
{
};
class fubar : public foo
{
};
foo *fn(void)
{
fubar *fu = new fubar;
return fu;
}

void main()
{
fubar *fu = fn();
}
    
the compiler complains because the "types are unrelated" [edited by - evilchicken on September 9, 2002 6:34:42 PM] [edited by - evilchicken on September 9, 2002 6:45:09 PM]

Share this post


Link to post
Share on other sites
Advertisement
You should have a virtual destructor defined for foo.

EDIT:
Also, you should NOT have a semicolon after:
class fubar : public foo


--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

[edited by - Matt Calabrese on September 9, 2002 6:37:17 PM]

Share this post


Link to post
Share on other sites
I tried the code below (MSVC 6.0, no SPs) and compiled with no errors or warnings. I think your problem lies elsewhere.

class foo
{
};

class foobar : public foo
{
};

foo * function()
{
foobar * fu = new foobar;
return fu;
}

int main()
{
foo * fu = function();
return 0;
}

Share this post


Link to post
Share on other sites
oh nevermind, in your int main() if you change foo to fubar (which is what I want) how come it doesnt work?

I can assign fubar to foo, but not foo to fubar.

right now I managed to work around the problem by making function return a void pointer and recasting it to the proper subclass. is there a less hackish way of doing this?

[edited by - evilchicken on September 9, 2002 7:50:55 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by evilchicken
I can assign fubar to foo, but not foo to fubar.

You can''t assign a pointer to an instance of the base class to a pointer to the derived class. Well, you can, but you''ll need to break a lot of things in the process.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!