🔒 Locked
detecting a sub-classed function?
Advertisement
Original Post
Is it possible to detect if a given class instance is responsible for handling a call to a specific virtual function?
Say I have class Parent and subclass Child. Both have the function Foo() each with their own version. I want any calls made to Foo() that are handled in the parent class to be able to detect if the subclass has a function definition for Foo or not, and if so then pass them to a child object.
Damn that soudns confusing. I think I asked the right question
So is it possible to detect the presence of a function in a class instance without it going to the parent class function if the function is not overridden? Or is there some hack I could use?
quote:I agree with you there.
Damn that soudns confusing
quote:Presuming I understand what you're saying, this is standard inheritance. The compiler/runtime should deal with all this for you (and transparently). In C++* the only way you can screw with this is if you dynamic_cast it (badly ) or introduce slicing through ByVal parameter passing. {edit: I'm pretty sure you can't do either of these using Java, not sure about C#/VB.Net tho}
I want any calls made to Foo() that are handled in the parent class to be able to detect if the subclass has a function definition for Foo or not, and if so then pass them to a child object.
hth
Jack
{edit: *providing that you've used the correct form of virtual modifiers}
[edited by - Jollyjeffers on May 11, 2004 12:30:21 PM]
What I have is a base class called Window and in each window there are pointers to child objects (all derived form window). A pointer to window* is used to call the function, but I want to have it so the pointers to children (which are all window*) are able to see if the child object has a overridden version of the same function. If it does, then pass it on. Dynamic casting is not much use here becaise I won''t know exactly what sort of subclass the child objects are.
I''m thinking this is impossible and I may need to look into alternatives...
I''m thinking this is impossible and I may need to look into alternatives...
quote:
I have a class called Parent.
I have a derived class from Parent called Child.
Sometime, I call function Foo().
If Child has function Foo(), then Foo() in Child is called.
Otherwise, Foo() in Parent is called.
That''s amost it, although it''s more...
I have a class called Parent.
I have a derived class from Parent called Child.
Sometime, I call function Foo() in the scope of Parent.
If Child has function Foo(), then Foo() in Child is called.
Otherwise, Foo() in Parent deals with the call.
As nagromo says, I only have pointers to the child object that are Parent* pointers.
This is classic polymorphism using virtual member functions.
Yours,
Archimage
(Ann-Marie Ratcliffe)
class Base
{
public:
Base() {};
virtual void DoSomething() { /* Base version */ };
};
class Derived : public Base
{
public:
Derived() {};
virtual void DoSomething() { /* Derived version */ };
};
int main()
{
Derived D; // Instantiate a derived class
Base* B = &D // Pointer of type Base, points to derived D
B->DoSomething(); // Will run D''s DoSomething)()
return 0;
}
Yours,
Archimage
(Ann-Marie Ratcliffe)
I know how standard virtual functions and polymorphism work, so I guess I haven''t made the problem clear enough.
In the example given by Archimage, the ONLY pointer I will have that points to the Derived object will exist inside an instance of Base. So I need the function call to go into the Base version of DoSomething and then it will determine if the object D is capable of handling this function and if yes, pass the call on. Otherwise Base should handle the call. The problem is, if I use regular polymorphism and simply call D->DoSomething() from within an instance of Base and D has no version of the DoSomething, the call will wind up back in Base and will enter into a infinite recursion loop.
So ideally I need a way to determine at run time if a dervied object has a derived function or not.
In the example given by Archimage, the ONLY pointer I will have that points to the Derived object will exist inside an instance of Base. So I need the function call to go into the Base version of DoSomething and then it will determine if the object D is capable of handling this function and if yes, pass the call on. Otherwise Base should handle the call. The problem is, if I use regular polymorphism and simply call D->DoSomething() from within an instance of Base and D has no version of the DoSomething, the call will wind up back in Base and will enter into a infinite recursion loop.
So ideally I need a way to determine at run time if a dervied object has a derived function or not.
Give it a different name?
Skizz
[edited by - Skizz on May 12, 2004 7:39:55 AM]
class Base
{
void Foo ()
{
// do some stuff
if (!Bar ())
{
// do other stuff
}
}
virtual bool Bar () {return false;}
};
class Derived : public Base
{
virtual bool Bar () {return true;}
};
int main (...)
{
Derived d;
Base *b = &d
b->Foo ();
}
Skizz
[edited by - Skizz on May 12, 2004 7:39:55 AM]
Yeah I thought about using boolean return values to do the checks, but I was kind of hoping to avoid that. It's a pain having to write an empty function that will simply return false in every derived class (of which there will be many). And you can guarantee I'll forget to stick it in one class, then spend three hours thinking the problem lies elsewhere when it fails to do it's job.
But right now, there doesn't seem to be much option as it would seem to be the consensus that there is no way to determine the existence of derived functions
addtional: I've decided to abandon my original idea of call propogation and am now working on redesigning the code so that the call propogation works backwards (ie using standard polymorphism). It's not as clean as I had hoped for, but as long as it works I guess it's alright.
[edited by - damocles on May 12, 2004 7:47:52 AM]
But right now, there doesn't seem to be much option as it would seem to be the consensus that there is no way to determine the existence of derived functions
addtional: I've decided to abandon my original idea of call propogation and am now working on redesigning the code so that the call propogation works backwards (ie using standard polymorphism). It's not as clean as I had hoped for, but as long as it works I guess it's alright.
[edited by - damocles on May 12, 2004 7:47:52 AM]
Just throwing out ideas atm, haven''t had a chance to think them through...
What about passing a bool as a parameter to the function to indicate if it is to process the ''Base'' if it doesn''t find a derived? So you call the parent with DoSomething(true) and it starts calling it''s children with DoSomething(false). In Base''s DoSomething(bool) if it is passed false it doesn''t call DoSomething on its children. DoSomething should return a bool to indicate if it was processed or not.
Archimage
What about passing a bool as a parameter to the function to indicate if it is to process the ''Base'' if it doesn''t find a derived? So you call the parent with DoSomething(true) and it starts calling it''s children with DoSomething(false). In Base''s DoSomething(bool) if it is passed false it doesn''t call DoSomething on its children. DoSomething should return a bool to indicate if it was processed or not.
Archimage
You don''t need to add empty functions to derived classes (or rather, a function that just returns false). If a derived class doesn''t define the function, the base class version is called instead, which returns false (the only time a derived class must implement a virtual function is when the base class defines it as a pure virtual function: virtual bool Bar () = 0;, but in the code I gave, the base class defines a default function). All implemented non base class versions return true.
Skizz
Skizz
Better still is:
Skizz
class Base
{
void Foo ()
{
// do some stuff
Bar ();
}
virtual void Bar ()
{
// do default stuff
}
};
class Derived : public Base
{
virtual void Bar ()
{
// do specific derived thing
}
};
class Derived2 : public Base
{
};
int main (...)
{
Derived d;
Derived2 d2
Base *b = &d
Base *b2 = &d2
b->Foo (); // this will call Derived::Bar
b2->Foo (); // this will call Base::Bar
}
Skizz
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement