I got an inheritance problem

Started by
6 comments, last by p-iddy 22 years, 4 months ago
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
Advertisement
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.
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.
The first one wouldn''t, I think the var would go out of scope as soon as the function exited.

------------
- outRider -
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:You should never return the address of a local variable


..unless it''s been given static storage.
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
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

This topic is closed to new replies.

Advertisement