   Function question
  Is there a way to call a function only if it exists.I have three classes ABCB and C inherit from A. Now I have a function that takes a class from type A. Obviously B or C is past.Now C has a function that B doesnt have. Can I call that function. Is there maybe something like if object is of type C doOr could I use try?-CProgrammer  
  The easiest way is to move that function (possibly pure) virtual in A, so that B and C get it: void MyFunction(A& a_object){   a_object.a_virtual_function();}but if that''s not possible, try this approach: void MyFunction(A& a_object){   try   { C& c_object = dynamic_cast<C>(a_object); c_object.c_member_function();   }   catch( std::bad_cast& e )   { std::cerr << "Not a C object" << std::endl;   }}Or, with pointers. void MyFunction(A* a_ptr){   C* c_ptr = dynamic_cast<C*>(a_ptr);   if( c_ptr != 0 ) c_ptr->c_member_function();   else std::cerr << "Not a C object" << std::endl;}Or the (easiest to handle, probably) hybrid version void MyFunction(A& a_object){   C* c_ptr = dynamic_cast<C*>(&a_object);   if( c_ptr != 0 ) c_ptr->c_member_function();   else std::cerr << "Not a C object" << std::endl;}Watch out that code that uses dynamic_cast is vastly more expensive than using a virtual function, especially if you end up doing a series of dynamic_casts to determine "Are you a B ? Do B stuff.", "Are you a C ? Do C stuff", etc.   “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 (C programming language co-inventor);  Hey I didnt know you could do that. Thanks for the input. I just thought of another way. I could have a variabel type in A and let B and C set it to a different value.What do you think. Is it less elegant.-CProgrammer   [edited by - CProgrammer on February 10, 2004 5:08:31 AM];quote:Original post by CProgrammerHey I didnt know you could do that. Thanks for the input. I just thought of another way. I could have a variabel type in A and let B and C set it to a different value.What do you think. Is it less elegant.It's definitely less elegant: virtual functions are designed to replace type fields.Instead of you  keeping track of a type field and doing explicit switch statements on it, you define your functions as virtual, and let the compiler itself keep track of each object's type and what version of the function to call.It's less work for you, and the compiler can usually generate better code, as it "knows" what usage it is intended for.   edit: note that, in any case, to get any kind of polymorphic treatment of your objects, you need to have at leastone virtual function in your classes.And if you intend to manipulate B or C objects through variables of type A (be they references or pointers), then A's destructor mustbe virtual.Let me mention again that the best solution is usually to place the as a virtual function of the base class. dynamic_cast is there for the unusual cases.“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 (C programming language co-inventor) [edited by - Fruny on February 10, 2004 5:19:52 AM];  What you are saying is this:I have an object, it could be an A, a B or a C.I don''t know which it is.I want them to behave differently.Some of them may do nothingSome of them may have default behaviourWhat you need is a function that will exist on all of them (ie in the base class).That function could do nothing, or call a default function in the base class, or call a function specific to the derived class.This is called overriding and needs the virtual keyword to work.It may be that the derived class wants to call a function that needs extra information.That information can either be stored on the derived class.Or it may be best to pass some context information to the base virtual function and then the derived class will know where to get any extra information it needs to pass to the class specific function.Hope that all helps.I could write out an example hierarchy if you want.note: dynamic_cast wouldn''t really be used for most programming cases as it can make designs cludgey. There is often a better way of doing it.;quote:Original post by Fruny   if you intend to manipulate B or C objects through variables of type A (be they references or pointers), then A's destructor mustbe virtual.Fruny, I don't think that's really right. You will run into problems if you want to call delete on a base pointer and the derived classes have destructors. Manipulating objects is fine and deleting them using the correct typed pointer is fine too.But generally, to be safe, you should always make the class destructor virtual if it is being inherited from.   [edited by - petewood on February 10, 2004 5:56:12 AM];  You are right that I am not right, but it keeps things simple in the novice mind After all, I was once taught you could not substract 3 from 2, that you could not divide 3 by 7, that you could not take the square root of a negative number, that multiplication was always commutative ...   “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 (C programming language co-inventor);quote:Original post by Fruny  You are right that I am not right, but it keeps things simple in the novice mind I thought it was out of character for you to get that wrong.''Everything should be made as simple as possible but not simpler''(c: ;  Thanks guys, those are some helpfull tips. Thnaks again.-CProgrammer 