quick question

Started by
6 comments, last by Daos 19 years, 8 months ago
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)
Advertisement
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.
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
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.
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();
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.
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
-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!

This topic is closed to new replies.

Advertisement