Sign in to follow this  

quick question

This topic is 4866 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 am checking my c++ book =P and tried to do some sort of graphical_apis wrapper class. If i put the real code it will be hard to understand so ill just write something easier:
class Parent
{  public:
      virtual bool functionA() = 0;
      bool functionB();
};

class Child : public Parent
{   public:
      bool functionA();
      double anotherFunction();
};

/* define the contents for each function 
(...)
   done */

int main()
{
   Parent* instance = new Child();
   instance->functionA(); //ok
   instance->functionB(); //ok
   instance->anotherFunction(); //error
}


i thought the pointer would let me use anotherFunction() but it seems i can only call functions that have been declared in the parent class. is it obligatory to use "Child* instance = new Child();" for it to work? (call the function declared in the child's body)

Share this post


Link to post
Share on other sites
You can't expect a parent* to know about anotherFunction [it isn't virtual, and inheritence is towards the inheritor, not inheritee], so yes, you will need a child instance or pointer to call that function.

Share this post


Link to post
Share on other sites
Quote:
Original post by giaym
is it obligatory to use "Child* instance = new Child();" for it to work? (call the function declared in the child's body)

Obviously you can declared a simple Child instance, but you may want to look into upcasting, specifically dynamic_cast [IIRC].
dynamic_cast<Child*>(instance)->anotherFunction(); //error

Share this post


Link to post
Share on other sites
Try to do this cast:
((Child*)instance)->anotherFunction(); //no more error
But this breaks the rule of "One directional inheritance"
this also goes with passing child classes(classes inherited from parent)to function with parent argument ,you can call any child function on that object.

Share this post


Link to post
Share on other sites
if you want to use dynamic_cast method, you should change your compiler's option of RTTI. in Visual Studio, it's position is in Project Setting->Category->C++ Language->Enable Run-time Type Identification(RTTI)

make it checked, it'll be ok

dynamic_cast<Child*>(instance)->anotherFunction();

Share this post


Link to post
Share on other sites
Quote:
Original post by Daos
Try to do this cast:
((Child*)instance)->anotherFunction(); //no more error
But this breaks the rule of "One directional inheritance"
this also goes with passing child classes(classes inherited from parent)to function with parent argument ,you can call any child function on that object.


Using C-style casts is a no-no, use the C++ family of cast operators, c-style casts are there for backward compatibility, the cast operators are mean't to be ugly as they give you an indication that there is probably a better/safer way.

Share this post


Link to post
Share on other sites
As a general rule: if you know you're going to need to use Child functions, use a Child (*) variable. An interface which expects a Parent input shouldn't ever have to use the Child-specific interface of its input (if present), and shouldn't care what kind of Parent (base or derived) it has received. Anything else is cognitively dissonant WRT the whole notion of polymorphism.

Naturally, this occasionally leads to design difficulties, due partly to the *language* design (specifically, the lack of "multiple dispatch". Fortunately there exist some design(a) patterns(b) intended to deal with the problems that arise.

Edit: Thanks to Extrarius, rating++. I should know well enough to put those quotes :s

Share this post


Link to post
Share on other sites
-doing a downcast(parent cast to child) using dynamic_cast is illegal so it's safer using dynamic_cast which does a run time check and returns 0 in case of bad cast.
-use
try{
}catch(bad_cast){
/*handle the error*/

}


-if you use static_cast the results are unpredictable(i usually use it with conversions between well known data types int ,float
,etc;be carefull though this could result in access violations if there's not enough space to hold the new data type(range))

I hate being quoted!

Share this post


Link to post
Share on other sites

This topic is 4866 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this