Archived

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

I got an inheritance problem

This topic is 5901 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

I'm not a proffesional programmer, but I thought I knew a thing or two about OOP. Here's my situation, I have a parent class and a child class, and a function that returns a pointer to a parent class. Now I know that you can do this: parentclass *ptr = new childclass; but the way I set up my program is like this: parentclass* function() { return *parentclassvariable; } childclass *ptr; ptr = function(); but I get an error. So what should I do, am I stuck and I have to redesign my program? Edited by - felisandria on November 19, 2001 10:42:17 AM

Share this post


Link to post
Share on other sites
Type cast.

childclass *ptr;
ptr = (childclass *)function();

This is generally bad design, though, since there may be attributes/operations that have not been initialized for the return value of function(). Perhaps you should look into virtual functions.

Share this post


Link to post
Share on other sites
In that program, you''re not returning a pointer to a parent class, but the parent class the pointer is pointing to. It should be either:

  
parentclass* function()
{
parentclass parentclassvariable;
return &parentclassvariable;
}


or...

  
parentclass* function()
{
parentclass* parentclassvariable;
return parentclassvariable;
}


I''m not sure about the first one, but the second one whould work fine.

Share this post


Link to post
Share on other sites
The first one wouldn''t, I think the var would go out of scope as soon as the function exited.

------------
- outRider -

Share this post


Link to post
Share on other sites
What you want is a downcast. You should use
  

class parentclass;
class childclass : public parentclass;

parentclass* parent = new childclass;
.
.
.
childclass* child = dynamic_cast<childclass*>( parent );



dynamic_cast will throw an exception if you try to downcast to a wrong type

  

class parentclass;
class childclass : public parentclass;
class otherchildclass : public parentclass;

parentclass* parent = new childclass;
.
.
.
otherchildclass* child = dynamic_cast<otherchildclass*>( parent ); // will throw a bad_cast exception




Be careful that dynamic casts require the use of RTTI (run-time time information : typeid and dynamic_cast), which is slow (relatively).



Masonium: You should never return the address of a local variable, it''s a guaranteed segfault, since the variable doesn''t exist outside of the function.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
You should never return the address of a local variable


..unless it''s been given static storage.

Share this post


Link to post
Share on other sites
the dynamic cast worked, but here''s where things get hairy in my program. You see, parent class is an abstract class (so it has pure virtual functions and I can''t actually create one of these.) and now I got this far:

Childclass *ptr;
ptr = dynamiccast(function());

But the way I implemented my program I have to do this:

ptr = new Childclass;
and I get an error because it says I''m creating a parent class

So is there any solution to this? or am I going to have to make the ptr a Parentclass

Share this post


Link to post
Share on other sites
Are you implementing all the pure virtual functions from the parent in the child?

What is it you are trying to do here, anyway? It looks very much like you''ve got the strong end of the wick (as Spooner would say), and your whole design is probably flawed

--

MP3 Dancer

Get A Stripper on your desktop

Share this post


Link to post
Share on other sites